0

I'm trying to read data from an array in JSON with JavaScript but I can't get it working. This is the segment of the JSON file from which I want to read the data, I want to read the age variable from different arrays:

{
    "failCount" : 1,
    "skipCount" : 15,
    "totalCount" : 156,
    "childReports" : 
    [
        {
            "result" : 
            {
                duration : 0.97834,
                empty : false,
                suites : 
                [
                    cases : 
                    [
                        {
                            "age" : 0,
                            "status" : Passed
                        }
                        {
                            "age" : 15,
                            "status" : Passed
                        }
                        {
                            "age" : 3,
                            "status" : failed
                        }
                    ]
                ]
            }
        }
    ]
}

I've tried this:

for (var i = 0; i < jsonData.childReports.suites.cases.length; i++) 
{
    var age = jsonData.childReports.suites.cases[i];
}

But it doesn't work. What would be the best way to do this?

Thanks in advance

5
  • 1
    Any error in console ? Commented Mar 18, 2016 at 9:39
  • Yes, I forgot to add that in the question. This is the error: Uncaught SyntaxError: Unexpected token o Commented Mar 18, 2016 at 9:40
  • Does JSON.parse(jsonData) work? Commented Mar 18, 2016 at 9:40
  • I tried that, but I can't get the data from the array. Commented Mar 18, 2016 at 9:41
  • Your JSON is wrong. Please fix it first. In " cases" array, objects are not separated by comma. Commented Mar 18, 2016 at 9:48

3 Answers 3

1

Try the following code:

  for (var i = 0; i < jsonData.childReports[0].result.suites[0].cases.length; i++) {
        var age = jsonData.childReports[0].result.suites[0].cases[i].age;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Correct Json:

{
"failCount" : 1,
"skipCount" : 15,
"totalCount" : 156,
"childReports" : [
{
"result" : {
    duration : 0.97834,
    empty : false,
        suites : [{
        cases : [
        {
        "age" : 0,
        "status" : "Passed"
        },
        {
        "age" : 15,
        "status" : "Passed"
        },
        {
        "age" : 3,
        "status" : "failed"
        }
    ]}
    ]
}
}]
}

Comments

0

This way you can achieve that :

var data = {
"failCount" : 1,
"skipCount" : 15,
"totalCount" : 156,
"childReports" : [
{
"result" : {
    duration : 0.97834,
    empty : false,
        suites : [{
        cases : [
        {
        "age" : 0,
        "status" : "Passed"
        },
        {
        "age" : 15,
        "status" : "Passed"
        },
        {
        "age" : 3,
        "status" : "failed"
        }
    ]}
    ]
}
}]
};

 for (var i = 0; i < data.childReports[0].result.suites[0].cases.length; i++) {
        console.log(data.childReports[0].result.suites[0].cases[i].age);
    }

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.