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.

1
  • Object.keys(JSON.parse(str)) is a good hint. Commented Mar 8, 2016 at 20:23

2 Answers 2

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]; 
}
Sign up to request clarification or add additional context in comments.

Comments

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

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.