The JSON syntax is a subset of the JavaScript syntax.
JSON Syntax Rules
JSON syntax is derived from 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:
Example
"name":"John"
JSON names require double quotes. JavaScript names don't.
JSON - Evaluates to JavaScript Objects
The JSON format is almost identical to JavaScript objects.In JSON, keys must be strings, written with double quotes:
JSON
{ "name":"John" }
JavaScript
{ name:"John" }
JSON Values
In JSON, values must be one of the following data types:- a string
- a number
- an object (JSON object)
- an array
- a boolean
- null
- a function
- a date
- undefined
JSON
{ "name":"John" }
JavaScript
{ name:'John' }
JSON Uses JavaScript Syntax
Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.With JavaScript you can create an object and assign data to it, like this:
Example
var person = { "name":"John", "age":31, "city":"New York" };
Example
// returns Johnperson.name;
Example
// returns Johnperson["name"];
Example
person.name = "Gilbert";
Example
person["name"] = "Gilbert";
JavaScript Arrays as JSON
The same way JavaScript objects can be used as JSON, JavaScript arrays can also be used as JSON.You will learn more about arrays as JSON later in this tutorial.
JSON Files
- The file type for JSON files is ".json"
- The MIME type for JSON text is "application/json"