ToolForge
ReferenceJSON

JSON Syntax Reference

A lookup page for the rules, rather than an explanation of the format. Six value types, one escape table, a handful of grammar constraints, and a section on the rules people believe exist but which the specification does not contain.

Everything here is checked against RFC 8259 and ECMA-404. If you want the format explained rather than tabulated, start with [what JSON is](/articles/what-is-json).

Written by toolforge.websitePublished Last reviewed How we build and check these tools

The six value types

Every value in a JSON document is exactly one of these.

TypeWritten asExample
ObjectBraces, comma-separated "name": value pairs{"id": 7, "ok": true}
ArrayBrackets, comma-separated values[1, "two", null]
StringDouble quotes only"hello"
NumberDecimal, optional fraction and exponent42, -1.5, 2.5e-3
BooleanLowercase literaltrue, false
NullLowercase literalnull

Notably absent: dates, binary data, integers as distinct from floats, sets, references, and any notion of a comment. Objects and arrays nest to any depth, and the two containers may hold values of mixed types — an array of objects and numbers is legal, if usually unwise.

Objects

{
  "key": "value",
  "nested": { "inner": 1 },
  "list": [1, 2, 3]
}

Rules:

  • Keys must be double-quoted strings. Unquoted keys are a JavaScript thing, not a JSON thing, and are the most common single cause of a parse failure.
  • A key may be any string, including the empty string, a string with spaces, or one containing punctuation. {"": 1} and {"user name": 1} are both valid.
  • No trailing comma after the last pair.
  • An empty object, {}, is valid.
  • Keys should be unique but are not required to be — see the surprises section below.
  • Member order carries no meaning, and parsers are not required to preserve it.

Arrays

[
  { "id": 1 },
  { "id": 2 }
]

Rules:

  • Elements are ordered, and that order is guaranteed — unlike object members.
  • Mixed types are permitted: [1, "a", null, {}, []] is valid.
  • No trailing comma after the last element.
  • An empty array, [], is valid.
  • There are no holes. [1, , 3] is a syntax error, where JavaScript would give you a sparse array.

Arrays are also the answer whenever order or duplication matters, since objects guarantee neither.

Strings and the escape table

Strings are wrapped in double quotes. Single quotes are never valid, anywhere — not for keys, not for values.

Two characters must be escaped inside a string, plus all control characters below U+0020:

EscapeCharacter
\"Double quote
\\Backslash
\/Forward slash (optional — allowed, never required)
\bBackspace, U+0008
\fForm feed, U+000C
\nLine feed, U+000A
\rCarriage return, U+000D
\tTab, U+0009
\uXXXXAny character, by four hex digits

Points worth knowing:

  • A literal newline inside a string is invalid. It must be written \n. This is why a multi-line SQL query or a PEM certificate pasted straight into a JSON string fails.
  • \uXXXX takes exactly four hex digits. Characters outside the Basic Multilingual Plane — emoji, most historic scripts — need a surrogate pair, two escapes together, so an emoji is \ud83d\ude00 rather than one escape.
  • You rarely need \u at all. JSON text is Unicode, conventionally UTF-8, so a string may contain é or 日本語 or 😀 directly.
  • The forward-slash escape is the odd one out: permitted but never necessary. It exists so JSON can be embedded in HTML without </script> terminating the surrounding tag early.

The mechanics of applying these in anger — nesting JSON inside JSON, or getting a payload through a shell — are in escaping strings in JSON.

Numbers

A number is an optional minus sign, an integer part, an optional fraction, and an optional exponent.

ValidInvalidWhy
0, -1, 42+1Leading plus is not permitted
1.5, -0.25.5The integer part is required
1e3, 2.5E-45.A fraction needs at least one digit
0.001, 007No leading zeros
0x1FNo hex, octal or binary
NaN, InfinityNot values in JSON
1_000No digit separators

The grammar imposes no limit on how many digits a number may have. Implementations do: RFC 8259 notes that good interoperability comes from expecting no more precision than IEEE 754 double precision provides, and that integers are interoperable in the range −(2^53)+1 to (2^53)−1 — roughly ±9.007 × 10^15.

Beyond that, digits are silently lost. A 19-digit identifier sent as a number arrives rounded in any JavaScript consumer, and nothing in the pipeline will warn you. Send large identifiers as strings.

NaN and Infinity deserve their own note because serialisers hit them constantly: JavaScript's JSON.stringify turns both into null rather than failing, so a division by zero somewhere upstream becomes a null in your data.

Document-level rules

  • Encoding. JSON text exchanged between systems must be encoded in UTF-8.
  • A byte-order mark is not allowed. Parsers commonly reject a leading BOM, which is why a file saved by a Windows editor as "UTF-8 with BOM" can fail on a first, invisible character. This is a genuinely nasty one to diagnose by eye.
  • Whitespace — space, tab, line feed, carriage return — is permitted between any two tokens and is otherwise meaningless. Indentation is for humans.
  • The top level may be any value. RFC 8259 defines a JSON text as "a serialized value", so "hello", 42 and true are each a complete valid document. Older parsers written against the 2006 specification may still require an object or array.
  • One document per text. Two objects side by side is not valid JSON. Streaming many records means one object per line — JSON Lines — which is a convention on top of JSON, not part of it.

Multiple documents: JSON Lines

Since a JSON text is exactly one value, a file containing two objects side by side is not valid JSON — which is a problem for logs and data exports, where you want to append records without rewriting the whole file.

The universal answer is JSON Lines, also written NDJSON: one complete JSON document per line, separated by \n.

{"level":"info","msg":"started","ts":"2026-09-07T09:00:00Z"}
{"level":"warn","msg":"retrying","ts":"2026-09-07T09:00:04Z"}
{"level":"error","msg":"gave up","ts":"2026-09-07T09:00:09Z"}

The rules are a convention on top of JSON rather than part of it:

  • Each line is an independent, complete JSON value — usually an object.
  • No line may contain a literal newline, so every record must be minified onto one line. Escaped \n inside strings is fine.
  • The file as a whole is not valid JSON, and parsing it whole will fail. Parse line by line.
  • UTF-8, and by convention a trailing newline at the end of the file.

Why it is worth knowing: it makes a file appendable and streamable. A process can write one more record without touching what came before, and a reader can process a hundred-gigabyte file with a few kilobytes of memory — neither of which a single JSON array permits. This is why structured application logs, database exports and machine-learning datasets are almost always in this shape.

The trade-off is that no JSON parser will read the file as a unit, so tooling has to know what it is looking at. If you need one file that is valid JSON, an array is the only option, and you accept that appending means rewriting.

Rules people think exist

Five widely-believed rules that the specification does not contain.

"Keys must be unique." RFC 8259 says they SHOULD be unique — a recommendation. Duplicates are legal, and the specification warns only that the receiving software's behaviour "is unpredictable". Most parsers keep the last one.

"JSON must start with { or [." True of the 2006 specification, not of RFC 8259. See above.

"Comments are allowed if the parser is lenient." Some parsers do accept them; that does not make the document JSON. There is no production for a comment in the grammar.

"A trailing comma is a warning." It is a hard syntax error, in both objects and arrays. MDN's documentation for JSON.parse demonstrates exactly this failure.

"Key order is preserved." Not guaranteed. Many parsers do preserve insertion order in practice, and JavaScript's does for string keys, but the specification explicitly notes that libraries differ over whether ordering is even visible.

The general shape of the mistake is assuming JSON is JavaScript with a different extension. It is a strict subset — everything JSON allows, JavaScript allows, but not the reverse:

SyntaxJavaScriptJSON
Unquoted key: {a: 1}YesNo
Single-quoted stringYesNo
Trailing commaYesNo
CommentsYesNo
undefined as a valueYesNo
Functions and methodsYesNo
NaN, InfinityYesNo
Hex, octal, 1_000YesNo
Leading +, or .5YesNo
Sparse array [1, , 3]YesNo
Backtick template stringYesNo
Duplicate keysYesYes, though discouraged

Every row in that table is a parse failure waiting to happen when object-literal text is pasted somewhere expecting JSON. The error-by-error guide covers what each one looks like when it does.

Checking a rule against real input

Reference tables settle arguments; a parser settles them faster. Paste the document into the parse tool and it either produces the parsed structure or the exact error, which is usually quicker than reading a table to work out which of two spellings is legal.

For a document rather than a fragment, the formatter reports the error message together with the line it came from, and will attempt a repair of the common structural mistakes — single quotes, bare keys, trailing commas — without touching the contents of your strings.

One caveat that applies to both, and to every other validator: a parser tells you the document is well-formed. It cannot tell you the data is correct. A payload with every field spelled wrong parses perfectly.

JSON Parse Tool

Parse JSON strings into JavaScript objects. It runs in your browser, so the files never leave your machine, and there is nothing to install or sign up for.

Try the JSON Parse Tool

Frequently asked questions

Can JSON keys contain spaces or special characters?

Yes. A key is just a string, so spaces, punctuation, emoji and the empty string are all legal. Your language may make such keys awkward to reach — bracket notation instead of dot notation — but the format permits them.

Is a single-quoted string ever valid in JSON?

No, in any position. Double quotes only, for both keys and string values. Single quotes are the second most common cause of a parse error after trailing commas, usually because the text started life as a JavaScript or Python literal.

How do I write a newline inside a JSON string?

As the two-character escape \n. A real line break inside a quoted string is a syntax error, which is why multi-line text — certificates, SQL, addresses — must be escaped before it can be embedded.

Does JSON support integers separately from floats?

No. There is one number type, and whether 42 becomes an integer or a float is decided by your parser, not by the document. This is why round-tripping through JSON can turn 1.0 into 1 — the trailing zero carries no information the format can represent.

What is the maximum nesting depth?

The specification sets none, but it warns implementations to expect limits, and parsers impose their own to avoid stack exhaustion. Documents nested tens of thousands of levels deep are a denial-of-service vector rather than a legitimate use.

The short version

Six value types, two mandatory escapes, no leading zeros, no trailing commas, UTF-8 without a BOM, and numbers you can trust to about fifteen digits. Most JSON problems are one of those rules being violated invisibly — and most of the remaining ones are a rule people assumed exists, like unique keys or preserved order, which the specification never promised.

Tools that go with this

Each one is here for a specific reason rather than because it is in the same category.

Popular

JSON Formatter

Format, validate, and beautify JSON online

Open Tool

Indents a document and points at the line a syntax error came from, which is the fastest way to check a rule against real input.

JSON Escape / Unescape

Escape and unescape JSON strings

Open Tool

Applies the escape rules in the table below mechanically, in both directions.

JSON Stringify Tool

Convert JavaScript objects to JSON strings

Open Tool

Shows what a serialiser produces from a value, including the parts of your data it silently drops.

JSON Schema Visualizer

Visualize JSON Schema structure and validation

Open Tool

Once the syntax is valid, this shows the shape a sample document actually has.

Sources

  1. RFC 8259 (STD 90) — The JavaScript Object Notation (JSON) Data Interchange Format

    IETF · Standards organisation · published 1 December 2017 · accessed 7 September 2026

    Cited for: The grammar, the escape set, UTF-8 requirement, the SHOULD-be-unique wording, and the interoperable integer range

  2. ECMA-404, 2nd edition — The JSON Data Interchange Syntax

    Ecma International · Standards organisation · published 1 December 2017 · accessed 7 September 2026

    Cited for: The syntax definition this reference tabulates, and that no semantics are implied by it

  3. JSON.parse() — JavaScript reference

    MDN Web Docs · Technical documentation · accessed 7 September 2026

    Cited for: Trailing commas and single-quoted strings being rejected, with the errors they raise

About the author

toolforge.website

Builder and maintainer, ToolForge

Started ToolForge in May 2026 and has built and maintained it since, writing every tool on the site and the documentation that goes with each one.

How we research and check this content · About ToolForge

Part of

JSON

What JSON is, the syntax rules that actually trip people up, why a parser rejected your file, and how JSON compares with XML and YAML.

Browse all developer tools
Buy Me a Coffee