[02] json syntax, 02 json
[02]
JSON syntax is a subset of JavaScript syntax.
JSON syntax rules
JSON syntax is a subset of the syntax of JavaScript Object Notation.
- Data in name/value pairs
- Data is separated by commas (,).
- Brackets save objects
- Square brackets Save the Array
JSON name/value pair
JSON data is written in the format of name/value pair.
Name/value pair includes the field name (in double quotation marks), followed by a colon, followed by a value:
"firstName":"John"
This is easy to understand. It is equivalent to this JavaScript statement:
firstName ="John"
JSON Value
The JSON value can be:
- Number (integer or floating point number) (no quotation marks required)
- String (in double quotation marks)
- Logical value (true or false) (no quotation marks required)
- Array (in square brackets)
- Object (in curly brackets)
- Null
JSON object
JSON objects are written in curly brackets:
The object can contain multiple name/value pairs:
{"firstName":"John","lastName":"Doe"}
This is easy to understand and is equivalent to this JavaScript statement:
firstName ="John" lastName ="Doe"
JSON Array
JSON array is written in square brackets:
An array can contain multiple objects:
{
"employees":[
{"firstName":"John","lastName":"Doe"},
{"firstName":"Anna","lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
}
In the preceding example, the object "employees" is an array containing three objects. Each object represents a record about someone (with a surname and a name.
JSON uses JavaScript syntax because JSON uses JavaScript syntax, JSON in JavaScript can be processed without additional software.
Using JavaScript, you can create an array of objects and assign values as follows:
var employees =[
{"firstName":"Bill","lastName":"Gates"},
{"firstName":"George","lastName":"Bush"},
{"firstName":"Thomas","lastName":"Carter"}
];
You can access the first entry in the JavaScript Object array as follows:
employees[0].lastName;
The returned content is:
Gates
You can modify the data as follows:
employees[0].lastName ="Jobs";
JSON File
- The JSON file type is ". json"
- The MIME type of JSON text is "application/json"