1

Having an issue with this JSON object....

{
    "1457458375537": {
        "message": "this is the message",
        "subject": "my subject"
    },
    "1457467436271": {
        "message": "test message",
        "subject": "hello"
    }
}

Basically for each object so the long number (e.g. 1457458375537), I want to loop through but am not sure how to reference that long number and loop through the full JSON object.

rlemon
  • 17,518
  • 14
  • 92
  • 123
Kieran Wild
  • 71
  • 10

2 Answers2

2
// data is all the json you gave in the example
for(var key in data){
    // keys are the numbers 
    // and inner are the objects with message and subject
    var inner = data[key]; 
}
Slava Shpitalny
  • 3,965
  • 2
  • 15
  • 22
1

Long numbers are keys in your json. You can loop through keys using Object.keys() function:

var data = {
    '1457458375537': {
        'message': 'this is the message',
        'subject': 'my subject'
    },
    '1457467436271': {
        'message': 'test message',
        'subject': 'hello'
    }
};

Object.keys(data).forEach(function(key) {
    console.log(key); // prints property name - long number
    console.log(data[key].message);
    console.log(data[key].subject);
});
madox2
  • 49,493
  • 17
  • 99
  • 99