JSON Syntax


JSON syntax is a subset of JavaScript syntax.


JSON Syntax Rules

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

  • Data is inname/valuepairs
  • Data by commas,separated
  • Use a backslash\to escape characters
  • Curly braces{}hold objects
  • Square brackets[]hold arrays, arrays can contain multiple objects

Two structures of JSON:

1. Object:Curly braces{}The object stored is an unorderedname/valuepair collection. An object begins with a left brace{starts, right brace}ends. Each "key" is followed by a colon:,name/valuepairs use commas,separated.

2. Array:Square brackets[]The stored array is an ordered collection of values. An array starts with a left square bracket[starts, right square bracket]ends, values use commas between,separated.

Values can be strings enclosed in double quotes, numbers, true, false, null, objects, or arrays, and they can be nested.


JSON Name/Value Pairs

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 a value:

"name" : "Example"

This is easy to understand, equivalent to this JavaScript statement:

name = "Example"

JSON Values

JSON values can be:

  • Numbers (integers or floating-point numbers)
  • Strings (in double quotes)
  • Booleans (true or false)
  • Arrays (in square brackets)
  • Objects (in curly braces)
  • null

JSON Numbers

JSON numbers can be integers or floating-point:

{ "age":30 }

JSON Objects

JSON objects in curly braces{}are written

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

Objects can contain multiple name/value pairs:

{ "name":"Example" , "url":"www.example.com" }

This is also easy to understand, equivalent to this JavaScript statement:

name = "Example" url = "www.example.com"


JSON Arrays

JSON arrays in square brackets[]are written

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":"Example" , "url":"www.example.com" }, { "name":"google" , "url":"www.google.com" }, { "name":"Weibo" , "url":"www.weibo.com" } ] }

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


JSON Booleans

JSON booleans can be true or false:

{ "flag":true }

JSON null

JSON can set null values:

{ "example":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":"example" , "url":"www.example.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 from 0):

sites[0].name;

The returned content is:

example

You can modify data like this:

sites[0].name="Example";

Try it yourself »
 

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


JSON Files

  • The file type of a JSON file is.json
  • The MIME type of JSON text isapplication/json
Other extensions