sjmartin sjmartin - 1 month ago 7
Javascript Question

What kind of data object is this format?

I was asked to take some JSON data to display but was given this format instead:

{size={XS=32, SM=34, MD=36, LG=38, XL=40, XXL=42}, length={XS=28, SM=30, MD=32, LG=34, XL=36, XXL=38}}


This isn't any sort of JSON object that i've seen before. Is it in another format i'm simply not aware of?

My goal is to take that data and create a table like so:



<table>
<tr>
<td>SM</td>
<td>MD</td>
<td>LG</td>
<td>XL</td>
<td>XXL</td>
</tr>
<tr>
<td>size</td>
<td>32</td>
<td>34</td>
<td>36</td>
<td>38</td>
<td>40</td>
<td>42</td>
</tr>
<tr>
<td>length</td>
<td>28</td>
<td>30</td>
<td>32</td>
<td>34</td>
<td>36</td>
<td>38</td>
</tr>
</table>





Thanks.

Answer

It's nearly JSON, replace = with :, put the double " in.

There is probably an easier way than I have done here, someone with regex skill will do a better job.

var x = "{size={XS=32, SM=34, MD=36, LG=38, XL=40, XXL=42}, length={XS=28, SM=30, MD=32, LG=34, XL=36, XXL=38}}";
var str = x.split('=').join('":').
  split('{').join('{"').split(', ').join(', "');
var y = JSON.parse(str);
console.log(y);

Comments