JSON Syntax
JSON syntax is a subset of JavaScript syntax.
JSON Syntax Rules
JSON syntax is a subset of JavaScript object notation syntax.
- Data is in
name/value
pairs - Data is separated by commas
,
- Use a backslash
\
to escape characters - Curly braces
{}
hold objects - Square brackets
[]
hold arrays, which can contain multiple objects
Two Structures in JSON:
1. Object: Curly braces {}
hold an object, which is a collection of unordered name/value
pairs. An object begins with a left brace {
and ends with a right brace }
. Each "key" is followed by a colon :
, and name/value
pairs are separated by commas ,
.
2. Array: Square brackets []
hold an array, which is an ordered collection of values. An array begins with a left bracket [
and ends with a right bracket ]
, with values separated by commas ,
.
Values can be strings (in double quotes), numbers, true, false, null, objects, or arrays, which can be nested.
JSON Name/Value Pairs
The format for JSON data is:
key : value
A name/value pair includes a field name (in double quotes), followed by a colon, and then the value:
"name" : "tutorialpro.org"
This is easily understood and equivalent to this JavaScript statement:
name = "tutorialpro.org"
JSON Values
JSON values can be:
- Numbers (integer or floating point)
- Strings (in double quotes)
- Logical values (true or false)
- Arrays (in square brackets)
- Objects (in curly braces)
- null
JSON Numbers
JSON numbers can be integers or floating points:
{ "age":30 }
JSON Objects
JSON objects are written within curly braces {}
:
{key1 : value1, key2 : value2, ... keyN : valueN }
Objects can contain multiple name/value pairs:
{ "name":"tutorialpro.org" , "url":"www.tutorialpro.org" }
This is also easy to understand and equivalent to this JavaScript statement:
name = "tutorialpro.org"
url = "www.tutorialpro.org"
JSON Arrays
JSON arrays are written within square brackets []
:
Arrays 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":"tutorialpro.org" , "url":"www.tutorialpro.org" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"微博" , "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 Values
JSON boolean values can be true or false:
{ "flag":true }
JSON null
JSON can set null values:
{ "tutorialpro":null }
JSON Uses JavaScript Syntax
Since 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":"tutorialpro" , "url":"www.tutorialpro.org" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"微博" , "url":"www.weibo.com" }
];
You can access the first item in the JavaScript object array (index starts at 0) like this:
sites[0].name;
The returned content is:
tutorialpro
You can modify data like this:
sites[0].name = "tutorialpro.org";
In the following section, you will learn how to convert JSON text into a JavaScript object.
JSON Files
The file type of JSON files is
.json
The MIME type of JSON text is
application/json