JSON Syntax Rules

beginner
9 min

JSON Syntax Rules

JSON looks forgiving, but it is actually one of the strictest formats you will work with. A single stray comma or an unquoted key will make a parser reject the entire document. In this lesson of our JSON tutorial series, we walk through every syntax rule you need to write valid JSON by hand and to debug documents that will not parse.

The Building Blocks

A JSON document consists of a single value. That value is usually an object or an array, but the specification also allows a lone string, number, boolean, or null at the top level.

json
{ "name": "CodeYourCraft", "founded": 2021, "topics": ["HTML", "CSS", "JSON"], "free": true, "sponsor": null }

Rule 1: Keys Must Be Double-Quoted Strings

Every key in a JSON object must be a string wrapped in double quotes. Single quotes and bare identifiers are invalid, even though JavaScript allows them in object literals.

json
{ "valid": true }

The following are all invalid JSON: { name: "x" } (unquoted key) and { 'name': "x" } (single quotes).

Rule 2: Strings Use Double Quotes and Escape Sequences

String values must also use double quotes. Special characters are written with a backslash escape:

  • \" for a double quote inside a string
  • \\ for a backslash
  • \n for a newline and \t for a tab
  • \u0041 for an arbitrary Unicode code unit

Rule 3: Numbers Follow a Strict Grammar

JSON numbers may be integers, decimals, or use exponent notation, such as 42, -3.14, and 6.02e23. However, JSON does not allow leading zeros (012), a bare leading decimal point (.5), NaN, Infinity, or hexadecimal literals like 0xFF.

Rule 4: Commas Separate, They Never Trail

Elements in arrays and pairs in objects are separated by commas, and a trailing comma after the last item is a syntax error. [1, 2, 3,] fails in every strict JSON parser, which surprises developers coming from modern JavaScript.

Rule 5: Only Six Value Types Exist

A value must be one of: string, number, object, array, true, false, or null. There are no dates, comments, functions, or undefined. If you need a date, store it as an ISO 8601 string like "2026-07-20T12:00:00Z".

Rule 6: No Comments Allowed

Standard JSON has no comment syntax. Lines starting with // or blocks in /* */ will break parsing. Some tools accept a relaxed superset called JSONC for configuration files, but never rely on comments when exchanging data between systems.

Whitespace Is Free

Spaces, tabs, and newlines between tokens are ignored, so you can format JSON however you like. Minified JSON on one line and pretty-printed JSON with indentation are exactly equivalent to a parser.

Validating Your JSON

When a document fails to parse, paste it into a validator such as jsonlint.com, or use your language runtime. In a browser console, JSON.parse throws a SyntaxError that includes the character position of the problem, which is often enough to spot the offending comma or quote.

Key Takeaways

  • Keys and strings must use double quotes; single quotes are invalid.
  • Trailing commas, comments, NaN, Infinity, and undefined are all forbidden.
  • Numbers cannot have leading zeros or hexadecimal notation.
  • Whitespace between tokens is ignored, so formatting is a style choice.

Next up: JSON Data Types, where we look closely at each of the six value types and how languages map them.

JSON Syntax Rules - JSON | CodeYourCraft | CodeYourCraft