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

Andreas
  • 5,393
  • 9
  • 44
  • 53
Sohan
  • 6,252
  • 5
  • 35
  • 56
  • 4
    That is straight forward JSON, any JSON parser will be able to parse it. – Quentin Apr 05 '13 at 10:55
  • jslint.com approves the json provided. [fiddle with console output of the parsed object](http://jsfiddle.net/nG9As/) – Imperative Apr 05 '13 at 10:56
  • I am not an UI,JS developer but thee is some requrement so struggling to built the logic arount it – Sohan Apr 05 '13 at 10:57
  • what have you tried ? http://mattgemmell.com/2008/12/08/what-have-you-tried/ – mpm Apr 05 '13 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. – Andrew Koper Sep 27 '18 at 00:20

3 Answers3

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]);
}
jwueller
  • 30,582
  • 4
  • 66
  • 70
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

Jacob George
  • 2,559
  • 1
  • 16
  • 28
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)
muneebShabbir
  • 2,500
  • 4
  • 29
  • 46
  • [$.parseJSON vs JSON.parse](http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse) the OP has no jQuery tag – Jacob George Apr 05 '13 at 11:09