0

I am unable to iterate the following JSON as I am expecting. The code is given as below :

$(document).ready(function() {
    var data = '{"employees":\n\
                [{"908887" : {"firstName":"John", "lastName":"Doe"}},\n\
                 {"98764" : {"firstName":"Anna", "lastName":"Smith"}},\n\
                 { "98762" : {"firstName":"Peter", "lastName":"Jones"}}]}';  

    var empObj = JSON.parse(data);
    for(var key in empObj.employees){
        alert('key - ' + key + '  value - ' + empObj.employees[key]);
    }   
});

In alert, I am getting the keys 0, 1, 2, but I want: 908887, 98764, 98762. I also want to iterate over the values.

Please tell how to solve the issue.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Learner
  • 261
  • 1
  • 4
  • 16

5 Answers5

4

Explaining the data you have.

employees is a array.

employees[0] is again a object.

employees[0].908887 is again a object with properties firstName and lastName.

So to get the keys 908887, 98764 etc.. you need to loop the array that is employees and then in each iteration you have a object, You need to extract the key from it.

var data = '{"employees":[{"908887" : {"firstName":"John", "lastName":"Doe"}},{"98764" : {"firstName":"Anna", "lastName":"Smith"}},{ "98762" : {"firstName":"Peter", "lastName":"Jones"}}]}';


var empObj = JSON.parse(data);

empObj.employees.forEach(function(value, index) {
  Object.keys(value).forEach(function(v, i) {
    console.log('key - ' + v + '\nvalue - ' + JSON.stringify(value[v]));
  });

});
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
1

You are iterating over an array.

To get the values you are asking about you need to iterate over each object in that array.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You need to iterate every object in array. Try this code.

$(document).ready(function(){

    var data = '{"employees":\n\
                 [{"908887" : {"firstName":"John", "lastName":"Doe"}},\n\
                  {"98764" : {"firstName":"Anna", "lastName":"Smith"}},\n\
                  { "98762" : {"firstName":"Peter", "lastName":"Jones"}}]}';  


    var empObj = JSON.parse(data);

    $.each(empObj.employees, function(keyEmp, valueEmp){
        $.each(valueEmp, function(keyIn, valueIn){
            console.log(keyIn);
            console.log(valueIn);
        })
    })

});

After executing the code, just check your developer console.

kishor10d
  • 543
  • 6
  • 24
1

This should do it:

var data = '{"employees":\n\
  [{"908887" : {"firstName":"John", "lastName":"Doe"}},\n\
   {"98764" : {"firstName":"Anna", "lastName":"Smith"}},\n\
   { "98762" : {"firstName":"Peter", "lastName":"Jones"}}]}';  


var empObj = JSON.parse(data);

empObj.employees.forEach((item) => {
  Object.entries(item).forEach(([key, val]) => {
    console.log(`key-${key}-val-${JSON.stringify(val)}`)
  });
});
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
1

var data = '{"employees":\n\
                                 [{"908887" : {"firstName":"John", "lastName":"Doe"}},\n\
                                  {"98764" : {"firstName":"Anna", "lastName":"Smith"}},\n\
                                  { "98762" : {"firstName":"Peter", "lastName":"Jones"}}]}';
var empObj = JSON.parse(data);

function flatObject(obj) {
    Object.keys(obj).forEach(y => {
        console.log('key: ' + y)
        if (obj[y] instanceof Object) {
            flatObject(obj[y]);
        } else {
            console.log('value: ' + obj[y])
        }
    });
}


empObj.employees.forEach(x => flatObject(x));
A.T.
  • 24,694
  • 8
  • 47
  • 65