Pankaj Dwivedi Pankaj Dwivedi - 6 months ago 22
JSON Question

How can iterate over JSON object and print its properties and their values?

I want to navigate each property in the JSON below in JavaScript. The below JSON contains two records for reference but in real time will have numerous such records.

{"Record_0":[{"Status":"CREATED","CreatorLoginId":"sandhya","Name":"G1"}],"Record_1":[{"Status":"CREATED","CreatorLoginId":"San","Name":"G2"}]}

I want to get the values of the fields "Status", "CreatorLoginId" and "Name" to assign them to something else.

How should I do it?

Max Max
Answer
            var myJSON = JSON.parse('{"Record_0":[{"Status":"CREATED","CreatorLoginId":"sandhya","Name":"G1"}],"Record_1":[{"Status":"CREATED","CreatorLoginId":"San","Name":"G2"}]}');
            for(var pr in myJSON)
            {
                console.log(myJSON[pr][0].Status);
                console.log(myJSON[pr][0].CreatorLoginId);
                console.log(myJSON[pr][0].Name);
            }