YouTip LogoYouTip

Json Syntax

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.

  • Data is in name/value pairs
  • Data is separated by commas ,
  • Use backslash to escape characters
  • Braces {} hold objects
  • Brackets [] hold arrays, which can contain multiple objects

Two structures of JSON:

1. Object: The object held by braces {} is an unordered collection of name/value pairs. An object starts with a left brace { and ends with a right brace }. Each "key" is followed by a colon :, and the name/value pairs are separated by commas ,.

Image 1

2. Array: The array held by brackets [] is an ordered collection of values. An array starts with a left bracket , and values are separated by commas ,.

Image 2

Values (value) can be strings (in double quotes), numbers, true, false, null, objects or arrays. They can be nested.

Image 3

JSON Name/Value Pairs

The format of JSON data is:

key : value
    

Name/value pairs include a field name (in double quotes), followed by a colon, then a value:

"name" : ""
    

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

name = ""
    

JSON Values

JSON values can be:

  • Numbers (integer or floating point)
  • Strings (in double quotes)
  • Boolean 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 braces {}:

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

An object can contain multiple name/value pairs:

{"name":"" , "url":"www."}
    

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

name = ""url = "www."
    

JSON Arrays

JSON arrays are written within brackets []:

[ { key1 : value1-1 , key2:value1-2 }, { key1 : value2-1 , key2:value2-2 }, { key1 : value3-1 , key2:value3-2 }, ... { key1 : valueN-1 , key2:valueN-2 }, ]
    

Example:

{"sites": [{"name":"" , "url":"www."}, {"name":"google" , "url":"www.google.com"}, {"name":"Weibo" , "url":"www.weibo.com"}]}
    

In the above example, 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 a null value:

{"":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 them like this:

Example

var sites = [{"name":"" , "url":"www."}, {"name":"google" , "url":"www.google.com"}, {"name":"Weibo" , "url":"www.weibo.com"}];
    

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

sites.name;
    

The returned content is:


    

You can modify the data like this:

sites.name="";
    

Try it Β»

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


JSON Files

  • The file type of a JSON file is .json
  • The MIME type of JSON text is application/json
← Json EvalDom Summary β†’