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.

0

5 Answers 5

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]));
  });

});

Sign up to request clarification or add additional context in comments.

Comments

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)}`)
  });
});

Comments

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.

Comments

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.

Comments

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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.