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.
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.
{
"name": "CodeYourCraft",
"founded": 2021,
"topics": ["HTML", "CSS", "JSON"],
"free": true,
"sponsor": null
}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.
{ "valid": true }The following are all invalid JSON: { name: "x" } (unquoted key) and { 'name': "x" } (single quotes).
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 unitJSON 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.
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.
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".
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.
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.
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.
NaN, Infinity, and undefined are all forbidden.Next up: JSON Data Types, where we look closely at each of the six value types and how languages map them.