X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JSON Syntax


The JSON syntax is a subset of the 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
  • Curly braces hold objects
  • Square brackets hold arrays

JSON Data - A Name and a Value

JSON data is written as name/value pairs.

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

"firstName":"John"


JSON Values

JSON values can be:

  • A number (integer or floating point)
  • A string (in double quotes)
  • A Boolean (true or false)
  • An array (in square brackets)
  • An object (in curly braces)
  • null

JSON Objects

JSON objects are written inside curly braces.

Just like in JavaScript, objects can contain multiple name/values pairs:

{"firstName":"John", "lastName":"Doe"}


JSON Arrays

JSON arrays are written inside square brackets.

Just like in JavaScript, an array can contain multiple objects:

"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]

In the example above, the object "employees" is an array containing three objects. Each object is a record of a person (with a first name and a last name).


JSON Uses JavaScript Syntax

Because JSON uses JavaScript syntax, no extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an array of objects and assign data to it like this:

Example

var employees = [
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName": "Jones"}
];

The first entry in the JavaScript object array can be accessed like this:

employees[0].firstName + " " + employees[0].lastName;

The returned content will be:

John Doe

The data can be modified like this:

employees[0].firstName = "Gilbert";

Try it yourself »

In the next chapter you will learn how to convert a JSON text to a JavaScript object.


JSON Files

  • The file type for JSON files is ".json"
  • The MIME type for JSON text is "application/json"


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook