Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have a json array as such (truncated here for brevity):

[
  {
    "name": null,
    "code": null,
    "results": [
      {
        "date": "2012-04-28T06:00:00.000Z",
        "name": null,
        "value": 135,
        "unit": "MG/DL",
        "code": null
      },
      {
        "date": "2012-04-28T06:00:00.000Z",
        "name": null,
        "value": 59,
        "unit": "MG/DL",
        "code": null
      }
    ]
  },
etc, etc,
]

I want to convert it to javascript array so I can extract the date and value to plot it. I've seen stuff about eval and JSON.parse but can't figure out how to access the values properly.

The json text is called labs so I do:

var obj = JSON.parse(labs);
alert("obj.length="+obj.length);    //correctly shows 22 objects
for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
        alert("prop: " +prop + " value: " +obj[prop]);
    }
}

The output is prop: 0 value:[object Object] prop: 1 value:[object Object] etc.

How do I get the date field and the value field?

share|improve this question
    
Just to clarify, you're looking to iterate over results within your array so you pull the individual date/value fields (like results[0]['date'], results[1]['date'], etc..)? –  SenorAmor Jan 22 '14 at 22:29

2 Answers 2

up vote 0 down vote accepted

You can retrieve the 1st "date" with:

obj[0].results[0].date

Based on that, you can nest loops for each 0. The 1st based on obj:

for (var i = 0; i < obj.length; i++) {
    // ...
}

The other based on each of the nested "results" within obj:

    var results = obj[i].results;

    for (var j = 0; j < results.length; j++) {
        // ...
    }

Within which you can alert the fields you're after:

        alert("Date: " + results[j].date + "\nValue: " + results[j].value);
share|improve this answer
    
Worked perfectly! All makes sense now. Thank you so much for taking the time to help. –  user3217883 Jan 23 '14 at 3:41

Wrap your loop in a regular old for loop. Right now you have an array of objects.

for (var i=0; i<obj.length; i++) {
    for (var prop in obj[i]) {
        if (obj[i].hasOwnProperty(prop)) {
            alert("prop: " +prop + " value: " +obj[i][prop]);
        }
    }
}
share|improve this answer
    
The outer loop correctly takes it through the 22 objects. The inner loop correctly picks up the name and code values. But when it gets to the Results object, it outputs a bunch of [object Object],[object Object], etc –  user3217883 Jan 23 '14 at 1:55
    
I'm not going to do all of the work for you. I thought it was pretty obvious how to get any inner objects or arrays or values since I provided the methods needed to access any particular inner element. –  tenub Jan 23 '14 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.