Converting a JSON Text to a JavaScript Object
Converting a JSON Text to a JavaScript Object
JSON is often used when data is sent from a server to a web page.
What is JSON?
* The JSON syntax is derived from JavaScript object notation syntax, but the
JSON format is text only. Code for reading and generating JSON data can be
written in any programming language.
JSON Example
JSON Example
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
The JSON format is syntactically identical to the code for creating JavaScript
objects.
Because of this similarity, a JavaScript program can easily convert JSON data
into native JavaScript objects.
"firstName":"John"
{"firstName":"John", "lastName":"Doe"}
JSON Arrays
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
Each object is a record of a person (with a first name and a last name).
Then, use the JavaScript built-in function JSON.parse() to convert the string
into a JavaScript object:
Example