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]"
}
}
]
To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.
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.
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.
The JSON. parse() method parses a string and returns a JavaScript object. The string has to be written in JSON format.
response[i].user.last_message.message.created_at
And here's a live demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With