Muhammad Riaz
Muhammad Riaz

Reputation: 67

printing key and value of json objects

i have 2 external json files and i have to read data from those files using the key and value. my application is not dependent on the obj.name etc. Please help me to print these values.

Planets.json

 {
"Planets": [
  { "name": "Mercury",
    "color": "Orange",
    "distance": "57.91 km",
    "radius": "2340 km",
    "year": "0.24085 Earth",
    "day": "88 days",
    "mass": "0.054 Earth"
  },

  { "name": "second",
    "color": "Orange",
    "distance": "57.91 km",
    "radius": "2340 km",
    "year": "0.24085 Earth",
    "day": "88 days",
    "mass": "0.054 Earth"
  }

}

Airports.json

    {
"Airports": [
  {
    "listing": "East 34th Street Heliport",
    "iata": "TSS",
    "type": "Heliport",
    "size": "Tiny"
  }

}

and this is the code i am trying.

              $.ajax({
                url:"planets.json",
                dataType: "json",
                success:function(json){
                    $.each(json, function(key, value){
                        console.log(key + ": " + value);
                    });
                }

            });

and i am getting this out put in console

Planets: [object Object]

Upvotes: 4

Views: 8734

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Get the nested object using json.Planets[0] and iterate over it.

$.each(json.Planets[0], function(key, value){
    console.log(key + ": " + value);
});

UPDATE 1 : If the object only contains one property then you can get it using Object.keys() and do the same as the previous example.

$.each(json[Object.keys(json)[0]][0], function(key, value){
    console.log(key + ": " + value);
});


UPDATE 2 : In case there are multiple properties then use $.each() method twice.

$.each(json,function(k, v){
    $.each(v[0], function(key, value){
        console.log(key + ": " + value);
    });
})

UPDATE 3 : If you have nested array with multiple objects then use an additional Array#forEach method to iterate it.

json[Object.keys(json)[0]].forEach(function(v){
    $.each(v, function(key, value){
        console.log(key + ": " + value);
    });
});

UPDATE 3 : If your JSON consists multiple properties with nested multiple array element do the combination of above methods.

$.each(json,function(k, v){
    v.forEach(function(v1){  
        $.each(v1, function(key, value){
            console.log(key + ": " + value);
        });
    });
})

Upvotes: 5

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, use below option

$.each(json, function(key, value) {
  $.each(value[0], function(k, val) {
    console.log(k + ":" + val);
  });
});

Codepen- http://codepen.io/nagasai/pen/GqGVGd

Upvotes: 1

Ricardo van Laarhoven
Ricardo van Laarhoven

Reputation: 788

You can use a double each so that its not depended on the name "airports"

$.each(json, function(objectTitle, objects){
    console.log('List of ' + objectTitle + ':');
    $.each(objects, function(objectKey, objectValue){
        console.log(objectKey + ':' +  objectValue);
    });
});

Upvotes: 0

Related Questions