Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท JSON in Depth

JSON Syntax

Learn the complete JSON grammar: objects, arrays, strings, numbers, booleans and null, the allowed whitespace, and the most common syntax mistakes, with live examples.

JSON (JavaScript Object Notation) is a tiny text format for structured data. Despite the name, it is language independent: nearly every language can read and write it. Its whole grammar fits on one page, and this lesson walks through all of it. Knowing the exact rules saves hours when a parser says "Unexpected token".

The six value types

A JSON value is exactly one of:

Type Example
object {"name": "Lena", "age": 30}
array [1, 2, 3]
string "hello"
number -12.5e3
boolean true or false
null null

A JSON document is a single value. Older parsers required the top level to be an object or an array; the current standard (RFC 8259) allows any value, so "hello" and 42 are valid documents.

Objects

An object is a comma-separated list of "key": value pairs in curly braces. Keys must be strings in double quotes. Values can be any JSON value, including nested objects.

JSON
{
  "id": 7,
  "name": "Lena",
  "address": { "city": "Oslo" },
  "tags": ["a", "b"]
}

Key order carries no meaning in the standard, and duplicate keys are discouraged: most parsers keep the last one.

Arrays

An array is an ordered, comma-separated list of values in square brackets. The values need not share a type, though consistent arrays are easier to work with.

JSON
[1, "two", true, null, [5, 6], {"seven": 7}]

Strings

Strings use double quotes only. Inside a string, backslash escapes are allowed:

Escape Meaning
\" double quote
\\ backslash
\/ slash (optional)
\b \f \n \r \t control characters
\uXXXX a UTF-16 code unit, e.g. รฉ is รฉ

A raw newline or tab inside a string is not allowed; it must be written as \n or \t. Characters outside the basic plane, such as emoji, can be written directly in UTF-8 or as a surrogate pair like ๐Ÿ˜€.

Numbers

JSON numbers are decimal: an optional minus sign, an integer part with no leading zeros, an optional fraction and an optional exponent. There is no hexadecimal, no +1, no .5, no NaN and no Infinity. More on this in JSON types and numbers.

Whitespace

Space, tab, line feed and carriage return are allowed between tokens, and nowhere else. Pretty printing is only decoration.

Try the grammar

Run valid and invalid documents through JSON.parse and read the error messages.

Node.js Valid and invalid JSON documents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The classic mistakes

  • Single quotes instead of double quotes.
  • Unquoted keys (valid in JavaScript objects, invalid in JSON).
  • A trailing comma after the last item.
  • Comments. JSON has none; see JSON pitfalls and variants for JSONC and JSON5.
  • Using undefined, NaN, Infinity, or functions as values.
Node.js Finding where JSON breaks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is valid JSON?

    Keys must be double-quoted strings, and trailing commas are not allowed.
  2. 2. Which value is NOT part of the JSON grammar?

    JSON has null but no undefined.
  3. 3. How do you put a newline inside a JSON string?

    Raw control characters are forbidden in strings, so write \n.
  4. 4. Are comments allowed in standard JSON?

    Standard JSON has no comments at all.

Summary

  • A JSON value is an object, array, string, number, boolean or null.
  • Keys and strings use double quotes; escapes start with a backslash.
  • No comments, trailing commas, single quotes or special numbers.
  • Parser error messages point to the offending position; read them.

Next: JSON types and numbers