Home  • Programming • JavaScript
JSON Syntax
JSON syntax is a subset of JavaScript syntax
JSON Syntax Rules JSON syntax is a subset of the JavaScript object notation syntax: 1 Data is in name/value pairs 2Data is separated by commas 3Curly braces hold objects 4Square brackets hold arrays JSON Name/Value Pairs
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"
This is simple to understand, and equals to the JavaScript statement:
firstName = "John"
JSON Values JSON values can be: 1 A number (integer or floating point) 2 A string (in double quotes) 3 A Boolean (true or false) 4An array (in square brackets) 5An object (in curly brackets) 6vnull JSON Objects 1JSON objects are written inside curly brackets. 2Objects can contain multiple name/values pairs.
{ "firstName":"John" , "lastName":"Doe" }
This is also simple to understand, and equals to the JavaScript statements:
firstName = "John"
lastName = "Doe"
JSON Arrays
JSON arrays are written inside square brackets.
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].lastName;
The returned content will be:
Doe
The data can be modified like this:
employees[0].firstName = "Jonatan";
JSON Files 1The file type for JSON files is ".json" 2The MIME type for JSON text is "application/json" In the next lesson you will learn how to convert a JSON text to a JavaScript object.

Comments 0


Copyright © 2025. Powered by Intellect Software Ltd