JavaScript JSON
JSON is a format used for storing and transferring data.
JSON is commonly used for transmitting data from the server to the web page.
What is JSON?
- JSON stands for JavaScript Object Notation.
- JSON is a lightweight format for data interchange.
- JSON is language independent.
- JSON is easy to understand.
| | * JSON uses JavaScript syntax, but the JSON format is just text. <br>The text can be read and used as a data format by any programming language. | | --- | --- |
JSON Example
The following JSON syntax defines a sites object: an array of 3 site records (objects):
JSON Example
{"sites":[
{"name":"tutorialpro", "url":"www.tutorialpro.org"},
{"name":"Google", "url":"www.google.com"},
{"name":"Taobao", "url":"www.taobao.com"}
]}
JSON Formatted as JavaScript Object
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.
JSON Syntax Rules
- Data is in key/value pairs.
- Data is separated by commas.
- Curly braces hold objects.
- Square brackets hold arrays.
JSON Data - A Name and a Value
JSON data is written as key/value pairs, similar to JavaScript object properties.
A key/value pair consists of a field name (in double quotes) followed by a colon, then by a value:
JSON Objects
JSON objects are contained within curly braces.
Like in JavaScript, objects can contain multiple key/value pairs:
JSON Arrays
JSON arrays are contained within square brackets.
Like in JavaScript, arrays can contain objects:
"sites":[
{"name":"tutorialpro", "url":"www.tutorialpro.org"},
{"name":"Google", "url":"www.google.com"},
{"name":"Taobao", "url":"www.taobao.com"}
]
In the above example, the object "sites" is an array containing three objects.
Each object represents site information (site name and URL).
Converting JSON String to JavaScript Object
Typically, we retrieve JSON data from a server and display the data in a web page.
For simplicity, we directly set a JSON string in our web page (you can also read our JSON Tutorial):
First, create a JavaScript string, with the data in JSON format:
var text = '{ "sites" : [' +
'{ "name":"tutorialpro" , "url":"www.tutorialpro.org" },' +
'{ "name":"Google" , "url":"www.google.com" },' +
'{ "name":"Taobao" , "url":"www.taobao.com" } ]}';
Then, use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript object:
var obj = JSON.parse(text);
Finally, use the new JavaScript object in your page:
Example
var text = '{ "sites" : [' +
'{ "name":"tutorialpro" , "url":"www.tutorialpro.org" },' +
'{ "name":"Google" , "url":"www.google.com" },' +
'{ "name":"Taobao" , "url":"www.taobao.com" } ]}';
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.sites[1].name + " " + obj.sites[1].url;
Related Functions
Function | Description |
---|---|
JSON.parse() | Used to convert a JSON string into a JavaScript object. |
JSON.stringify() | Used to convert a JavaScript value to a JSON string. |
For more information on JSON, you can read our JSON Tutorial.