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

JSON Syntax

JSON Syntax


JSON syntax is a subset of JavaScript syntax.


JSON syntax rules

JSON syntax is a subset of the JavaScript object notation syntax.

The two structures of JSON:

1. Object:An object saved in curly braces {} is an unorderedname/valuecollection of pairs. An object starts with a left brace { and ends with a right brace }. Each "key" is followed by a colon :, name/valueand pairs are separated by commas.

**2. Array:** An array saved in square brackets [] is an ordered collection of values. An array starts with a left square bracket [ and ends with a right square bracket ], with values separated by commas.

A value can be a double-quoted string, a number, true, false, null, an object, or an array, and they can be nested.


JSON name/value pair

The writing format of JSON data is:

key : value

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

"

name

"

:

"

Runoob Tutorial

"

This is easy to understand; it is equivalent to this JavaScript statement:

name

=

"

Runoob Tutorial

"


JSON values

JSON values can be:


JSON numbers

JSON numbers can be integers or floating point:

{

"

age

"

:

30

}


JSON Objects

A JSON object is written in curly braces {}:

{key1 : value1, key2 : value2, ... keyN : valueN }

An object can contain multiple name/value pairs:

{

"

name

"

:

"

Runoob Tutorial

"

,

"

url

"

:

"

www.runoob.com

"

}

This is also easy to understand; it is equivalent to this JavaScript statement:

name

=

"

Runoob Tutorial

"

url

=

"

www.runoob.com

"


JSON Arrays

A JSON array is written in square brackets []:

An array can contain multiple objects:

[

    { key1 : value1-1 , key2:value1-2 }, 

    { key1 : value2-1 , key2:value2-2 }, 

    { key1 : value3-1 , key2:value3-2 }, 

    ...

    { key1 : valueN-1 , key2:valueN-2 }, 

]

{

"

sites

"

:

[

{

"

name

"

:

"

Runoob Tutorial

"

,

"

url

"

:

"

www.runoob.com

"

}

,

{

"

name

"

:

"

google

"

,

"

url

"

:

"

www.google.com

"

}

,

{

"

name

"

:

"

Weibo

"

,

"

url

"

:

"

www.weibo.com

"

}

]

}

In the example above, the object sites is an array containing three objects. Each object represents a record about a website (name, url).


JSON boolean

A JSON boolean can be true or false:

{

"

flag

"

:

true

}


JSON null

JSON can set a null value:

{

"

runoob

"

:

null

}


JSON uses JavaScript syntax

Because JSON uses JavaScript syntax, no additional software is needed to process JSON in JavaScript.

With JavaScript, you can create an array of objects and assign values like this:

Example

var

sites

=

[

{

"

name

"

:

"

runoob

"

,

"

url

"

:

"

www.runoob.com

"

}

,

{

"

name

"

:

"

google

"

,

"

url

"

:

"

www.google.com

"

}

,

{

"

name

"

:

"

Weibo

"

,

"

url

"

:

"

www.weibo.com

"

}

]

;

You can access the first item in the JavaScript object array like this (index starts at 0):

sites

[

0

]

.

name

;

The returned content is:

runoob

You can modify the data like this:

sites

[

0

]

.

name

=

"

Runoob Tutorial

"

;

Try it »

 

In the following chapters, you will learn how to convert JSON text into a JavaScript object.


JSON files