Skip to content

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.

  • 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 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 ("").

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 (true or false - 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 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 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.

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 name
console.log(employees[0].firstName); // Output: Alice
// Accessing the second employee's last name using bracket notation
console.log(employees[1]["lastName"]); // Output: Smith

Modifying data is also standard JavaScript:

employees[0].firstName = "Alicia";
console.log(employees[0].firstName); // Output: Alicia

The next chapters cover how to convert between JSON text and JavaScript objects, and how to fetch JSON data.

  • The standard file extension for JSON files is .json.
  • The official MIME type for JSON text is application/json.