Q:

Iterate through Json Object to get first object

I have a JSON object like this:
{"2":[{"id":1,"index":"1.0000"},{"id":2,"index":"2.0000"}]}

How do I loop through the object.
I tried like this:
$.each(jsonObject, function (i, data)
{
   $.each(data, function (i, innerData)
   {
        console.log(JSON.stringify(innerData));
   });
});

This is what I get:
{"id":1,"index":"1.0000"}
{"id":2,"index":"2.0000"}

How do I get value 2 from {"2":[{"id":1,"index":"1.0000"},{"id":2,"index":"2.0000"}]}

A:

You can get is using Object.keys(), this will retrieve object keys as an array.

var jsonObject = {
  "2": [{
    "id": 1,
    "index": "1.0000"
  }, {
    "id": 2,
    "index": "2.0000"
  }]
};

var key = Object.keys(jsonObject)[0];

document.write(key)

$.each(jsonObject[key], function(i, innerData) {
  console.log(JSON.stringify(innerData));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

