1

I would like to parse a complex JSON structure but struggling with it as it is not straight a forward JSON. Here is the sample:

[ { "target":"collectd.matrix.oracle.avg_resp_time", "datapoints":[ [8.0, 1365158480], [null, 1365158490], [null, 1365158500], [null, 1365158510], [null, 1365158520], [null, 1365158530], [8.0, 1365158540], [null, 1365158550], [null, 1365158560], [null, 1365158570], [null, 1365158580], [null, 1365158590], [8.0, 1365158600], [null, 1365158610], [null, 1365158620], [null, 1365158630], [null, 1365158640], [null, 1365158650], [8.0, 1365158660], [null, 1365158670], [null, 1365158680], [null, 1365158690], [null, 1365158700], [null, 1365158710], [null, 1365158720], [null, 1365158730], [null, 1365158740], [null, 1365158750], [null, 1365158760], [null, 1365158770] ] } ] 

I want to capture the value of each field like eg:X=8.0,Y=1365158540 and need some help or logic to parse this.

Thanks, sohan

5
  • 4
    That is straight forward JSON, any JSON parser will be able to parse it. Commented Apr 5, 2013 at 10:55
  • jslint.com approves the json provided. fiddle with console output of the parsed object Commented Apr 5, 2013 at 10:56
  • I am not an UI,JS developer but thee is some requrement so struggling to built the logic arount it Commented Apr 5, 2013 at 10:57
  • what have you tried ? mattgemmell.com/2008/12/08/what-have-you-tried Commented Apr 5, 2013 at 10:58
  • 1
    The question is a good one. It is not a simple array of objects, like all the tutorials show how to loop through. There are a different structures in the array. Commented Sep 27, 2018 at 0:20

3 Answers 3

2
var jsonData = JSON.parse(data) 

where

data = '[{"target": "collectd.matrix.oracle.avg_resp_time", "datapoints": [[8.0, 1365158480], [null, 1365158490], [null, 1365158500], [null, 1365158510], [null, 1365158520], [null, 1365158530], [8.0, 1365158540], [null, 1365158550], [null, 1365158560], [null, 1365158570], [null, 1365158580], [null, 1365158590], [8.0, 1365158600], [null, 1365158610], [null, 1365158620], [null, 1365158630], [null, 1365158640], [null, 1365158650], [8.0, 1365158660], [null, 1365158670], [null, 1365158680], [null, 1365158690], [null, 1365158700], [null, 1365158710], [null, 1365158720], [null, 1365158730], [null, 1365158740], [null, 1365158750], [null, 1365158760], [null, 1365158770]]}]'; 

jsonData[0]['datapoints'] is the array of all the datapoints

Reference for JSON.parse

Sign up to request clarification or add additional context in comments.

Comments

2

The native JSON.parse() should work just fine. Use json2.js for backwards compatibility in older browsers. Here is an example:

var data = JSON.parse(yourJsonGoesHere), datapoints = data[0].datapoints, i; for (i = 0; i < datapoints.length; ++i) { console.log('x:' + datapoints[i][0] + ', y:' + datapoints[i][1]); } 

Comments

1

you can just add datatype:json in your ajax call if you are getting response via ajax

OR

you can use http://api.jquery.com/jQuery.parseJSON/

var obj = jQuery.parseJSON(jsonString) 

1 Comment

$.parseJSON vs JSON.parse the OP has no jQuery tag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.