Search code examples
javascriptjsongetjson

Reading nested values within json file - with JS


I'm trying to read values from a JSON file. So far I can read down to stopTime but I cannot work out how to read the nested values after that.

My JSON file

 "description": [
    {
    "id": " an id number",
    "something 1": "xxxx",
    "something 2": "yyyy",
    "startTime": "2017-12-27T18:47:15.000+0000",
    "stopTime": "2017-12-27T18:47:15.000+0000",
    "mediaFiles": [
        {
        "mediaUri": " some url value",
        "mediaPath": "some path value",
        "startTime": " a time",

My JS code which works.

var J = [];
$.getJSON( "calls/jsonfile.json", function( data){
  J = data;
  var x = 48;
  for(x=0;x<48;++x) {
     var id = J.description[x].id;
     ...
  }
}

This works well and i get the values i'm looking for.

Any help greatly appreciated... I'm pretty new to this stuff.


Solution

  • for(x=0;x<48;++x) {
        var id = J.description[x].id;
        // You can loop over the mediaFiles like this:
        for( num in J.description[x].mediaFiles ) {
            var theURI  = J.description[x].mediaFiles[num].mediaUri
            var thePath = J.description[x].mediaFiles[num].mediaPath
            ...
        }
        ...
    }