Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript JSON

JavaScript JSON


JSON is a format for storing and transporting data.

JSON is often used to send data from a server to a web page.


What Is JSON?

| Note | * JSON uses JavaScript syntax, but the JSON format is just text.
The text can be read by any programming language and passed as a data format. | | --- | --- |

JSON example

The following JSON syntax defines the sites object: an array of 3 website entries (objects):

JSON example

{

"

sites

"

:

[

{

"

name

"

:

"

Runoob

"

,

"

url

"

:

"

www.runoob.com

"

}

,

{

"

name

"

:

"

Google

"

,

"

url

"

:

"

www.google.com

"

}

,

{

"

name

"

:

"

Taobao

"

,

"

url

"

:

"

www.taobao.com

"

}

]

}


JSON formatted as a JavaScript object

The JSON format is syntactically identical to the code for creating JavaScript objects.

Because they are so similar, a JavaScript program can easily convert JSON data into JavaScript objects.


JSON syntax rules


JSON Data - A Name Corresponds to a Value

JSON data is written as key/value pairs, just like JavaScript object properties.

A key/value pair includes a field name (in double quotes), followed by a colon, then the value:

"name":"Runoob"


JSON Objects

JSON objects are written inside curly braces.

Just like in JavaScript, an object can hold multiple key/value pairs:

{"name":"Runoob", "url":"www.runoob.com"}


JSON Arrays

JSON arrays are written inside square brackets.

Just like in JavaScript, an array can contain objects:

"

sites

"

:

[

{

"

name

"

:

"

Runoob

"

,

"

url

"

:

"

www.runoob.com

"

}

,

{

"

name

"

:

"

Google

"

,

"

url

"

:

"

www.google.com

"

}

,

{

"

name

"

:

"

Taobao

"

,

"

url

"

:

"

www.taobao.com

"

}

]

In the example above, the object "sites" is an array containing three objects.

Each object is information about a site (site name and site address).


Convert a JSON String to a JavaScript Object

Usually we read JSON data from a server and display the data in a web page.

For simplicity, we set a JSON string directly in the web page (you can also read our JSON Tutorial):

First, create a JavaScript string containing data in JSON format:

var

text

=

'

{ "sites" : [

'

'

{ "name":"Runoob" , "url":"www.runoob.com" },

'

'

{ "name":"Google" , "url":"www.google.com" },

'

'

{ "name":"Taobao" , "url":"www.taobao.com" } ]}

'

;

Then, use the built-in JavaScript 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":"Runoob" , "url":"www.runoob.com" },

'

'

{ "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

;

Try it »


Related functions

function Description
JSON.parse() Used to convert a JSON string into a JavaScript object.
JSON.stringify() Used to convert a JavaScript value into a JSON string.

For more information about JSON, you can read our JSON Tutorial。