Giedrius Giedrius - 2 months ago 12
JSON Question

Formatting json properties

I have a JSON file with thousands of lines, which i need to have in another format.

My current code looks like that:

[{"Date":"2012-05-11","Value":19.5},
{"Date":"2012-05-15","Value":19.5},
{"Date":"2012-05-16","Value":18},
{"Date":"2012-05-17","Value":17.75},
...]


And it should look like this:

[Date.UTC(2012,5,11),19.5],
[Date.UTC(2012,5,13),19.5],
[Date.UTC(2012,5,16),18],
[Date.UTC(2012,5,17),17.75],
....]


Is there a tool or a quick way to do this, changing thousands of lines manually would take too much time.

Thanks in advance.

Answer

Programmatically, say php for instance, and treating each line of your JSON file as a string, you could use preg_replace(), but since it's a one time requirement, simply load your json file into a text editor and:

Delete the leading and ending square braces.

Find all {"Date":" replace with [Date.UTC(

Find all ","Value": replace with ),

Find all }, replace with ],

Save