Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access data in a JSON object (parsed) in JavaScript

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]"
        }
    }
]
like image 629
Jonathan Clark Avatar asked Apr 16 '11 22:04

Jonathan Clark


People also ask

How can I access data from JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How do you parse JSON in JavaScript?

parse() JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.

Can we extract the data from JSON file?

To extract the name and projects properties from the JSON string, use the json_extract function as in the following example. The json_extract function takes the column containing the JSON string, and searches it using a JSONPath -like expression with the dot . notation. JSONPath performs a simple tree traversal.

What does JSON parse () return?

The JSON. parse() method parses a string and returns a JavaScript object. The string has to be written in JSON format.


1 Answers

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

And here's a live demo.

like image 151
Darin Dimitrov Avatar answered Sep 17 '22 16:09

Darin Dimitrov