JSON Syntax
JSON Syntax
Section titled “JSON Syntax”JSON (JavaScript Object Notation) syntax is a lightweight format for storing and transporting data. It’s easy for humans to read and write, and easy for machines to parse and generate.
JSON syntax is derived from JavaScript object literal syntax, but it has stricter rules.
Core JSON Syntax Rules
Section titled “Core JSON Syntax Rules”- Data is represented in name/value pairs.
- Data is separated by commas
,. - Objects are enclosed in curly braces
{}. - Arrays are enclosed in square brackets
[]. - Names (keys) must be strings enclosed in double quotes
"". - Values must be one of the allowed JSON data types.
JSON Data - Name/Value Pairs
Section titled “JSON Data - Name/Value Pairs”JSON data consists of name/value pairs, similar to properties in JavaScript objects or key-value pairs in dictionaries/maps in other languages.
A name/value pair consists of a field name (key), followed by a colon :, followed by a value:
"firstName": "Alice"Important: Unlike JavaScript object literals where keys can sometimes be unquoted, JSON keys must always be strings enclosed in double quotes ("").
JSON Values
Section titled “JSON Values”A value in JSON can be one of the following data types:
- A string (enclosed in double quotes
"", supporting Unicode and backslash escapes like\n,\t). - A number (integer or floating-point, no distinction like in JavaScript. Octal and hexadecimal formats are not allowed).
- An object (enclosed in curly braces
{}). - An array (enclosed in square brackets
[]). - A boolean (
trueorfalse- lowercase). - The value
null(lowercase).
JSON does not support functions, dates (typically represented as strings in ISO 8601 format), undefined, comments, or trailing commas.
JSON Objects
Section titled “JSON Objects”JSON objects represent collections of name/value pairs. They start with { and end with }. Each name is followed by : and the name/value pairs are separated by ,.
Example of a JSON object:
{ "firstName": "Bob", "lastName": "Smith", "age": 30, "isStudent": false, "address": { "street": "123 Main St", "city": "Anytown" }}JSON Arrays
Section titled “JSON Arrays”JSON arrays represent ordered lists of values. They start with [ and end with ]. Values are separated by ,.
Example of a JSON array containing strings:
["apple", "banana", "cherry"]Arrays can contain values of different types, including other arrays or objects:
{ "department": "Sales", "employees": [ { "firstName": "Alice", "lastName": "Green" }, { "firstName": "Bob", "lastName": "Smith" }, { "firstName": "Charlie", "lastName": "Brown" } ]}In this example, the value associated with the key "employees" is an array of three JSON objects.
JSON and JavaScript
Section titled “JSON and JavaScript”Because JSON’s syntax is a subset of JavaScript’s object literal syntax, working with JSON in JavaScript is straightforward.
You can easily parse a JSON string into a JavaScript object using JSON.parse(), and serialize a JavaScript object into a JSON string using JSON.stringify().
Consider this JavaScript object:
const employees = [ { "firstName": "Alice", "lastName": "Green" }, { "firstName": "Bob", "lastName": "Smith" }];Accessing data is standard JavaScript:
// Accessing the first employee's first nameconsole.log(employees[0].firstName); // Output: Alice
// Accessing the second employee's last name using bracket notationconsole.log(employees[1]["lastName"]); // Output: SmithModifying data is also standard JavaScript:
employees[0].firstName = "Alicia";console.log(employees[0].firstName); // Output: AliciaThe next chapters cover how to convert between JSON text and JavaScript objects, and how to fetch JSON data.
JSON Files and MIME Type
Section titled “JSON Files and MIME Type”- The standard file extension for JSON files is
.json. - The official MIME type for JSON text is
application/json.