larryq
larryq

Reputation: 16299

Transforming this JSON via underscore or a javascript utility library?

I have some JSON that looks as so:

[{"target": "mydata.12.2", "datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}]

I'd like to remove the first key and surrounding array brackets so it reads:

{"datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}

My javascript parsing skills leave a bit to be desired and I was hoping someone could help. Perhaps underscore.js has something that could assist here?

Upvotes: 0

Views: 88

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382132

No need for a library. Supposing your first array is called obj1, you just have to do

var obj2 = obj1[0];
delete obj2['target'];

Note that you seem to have no JSON here, just plain javascript objects.

Supposing you really want to start from JSON and get some JSON at end, parse then stringify :

var obj2 = JSON.parse(json1);
delete obj2['target'];
var json2 = JSON.stringify(obj2);

Upvotes: 4

Related Questions