7

I need to get the data "messages" from this JSON object. How can I do it in JavaScript?

To access for example the lastname I just use:

response[i].user.lastname

But how can I access the messages?

[
    {
        "user": {
            "last_message": {
                "message": {
                    "created_at": "2011-04-16T16:40:56Z",
                    "updated_at": "2011-04-16T16:40:56Z",
                    "to": null,
                    "id": 10,
                    "user_id": 28,
                    "message": "This is a message"
                }
            },
            "nickname": "thenicky",
            "id": 28,
            "lastname": "white",
            "firstname": "Sean",
            "bio": "A short bio",
            "email": "[email protected]"
        }
    }
]

3 Answers 3

8
response[i].user.last_message.message.created_at

And here's a live demo.

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

3 Comments

I get this error when I try it: Result of expression 'response[i].user.last_message' [undefined] is not an object.
@Jonathan Clark, works fine for me. Checkout the following live demo: jsfiddle.net/Dwxj2/1
Works fine now. It was a problem with cache. Thanks!
3

response[i].user.last_message.message

1 Comment

What a fantastic answer. This deserves more recognition!
2

Some examples:

alert(response[0].user.last_message.message.id);    
alert(response[0].user.nickname);    
alert(response[0].user.lstname); 

and working code:

JSFiddle Working Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.