JSON Specification: Standards Behind Syntax Validation

What the JSON standard (RFC 8259 and ECMA-404) allows, why strict validators reject common inputs, and when formatting or re-serializing JSON changes its meaning.

A JSON validator is only as useful as the standard it checks against. Many "invalid JSON" errors come from habits that look harmless, such as comments, trailing commas or single quotes. Other problems pass validation and then change meaning when a tool reformats the data. This review explains what the JSON specification defines, which inputs it rejects, and how to tell whether formatting, minifying or re-serializing JSON keeps it intact for your purpose.

Which documents define JSON

Two documents define JSON: Ecma International's ECMA-404 and the IETF's RFC 8259, which is also designated Internet Standard STD 90. They describe the same grammar. ECMA-404 covers syntax only. The RFC adds interoperability guidance on text encoding, duplicate object names and number precision. RFC 8259 replaces the earlier RFC 4627 and RFC 7159, so a validator built on the oldest of these may behave differently on edge cases, as noted below.

Several related formats are not part of the core standard. I-JSON (RFC 7493) is a stricter profile that, among other rules, forbids duplicate names. JSON Schema is a separate specification for describing the expected structure and types of a document. JSON5, "JSON with comments" and JSON Lines are different formats with their own rules. A file that one of these accepts is not necessarily valid JSON.

What the grammar allows

  • Values: object, array, string, number, and the literals true, false and null, written in lowercase.
  • Strings: enclosed in double quotes. Control characters U+0000 through U+001F must be escaped, so a raw tab or line break inside a string is invalid. Escapes include \n, \t, ", \ and \uXXXX. Characters outside the Basic Multilingual Plane are escaped as surrogate pairs.
  • Numbers: an optional minus sign, an integer part with no leading zeros, an optional fraction and an optional exponent. A leading plus sign, hexadecimal notation, a bare decimal point such as .5, NaN and Infinity are not allowed.
  • Objects: name/value pairs whose names are strings. The RFC says names SHOULD be unique and treats objects as unordered collections.
  • Whitespace: only space, horizontal tab, line feed and carriage return are permitted between tokens.
  • Top level: under RFC 8259 and ECMA-404, any value can be the entire document. RFC 4627 required an object or an array, and some older validators still enforce that rule.
  • Encoding: JSON exchanged between systems that are not part of a closed ecosystem must be UTF-8. Implementations must not add a byte order mark. Parsers may ignore one, but they are not required to.
{
  "name": "Ada",
  "active": true,
  "score": 9.5e1,
  "tags": ["a", "b"],
  "manager": null,
  "note": "Line one\nLine two"
}

The document above is valid under both standards. The line break in the note field is written as the escape sequence \n, not as an actual line break inside the quotes.

Common inputs that fail strict validation

Input | Valid under RFC 8259? | Reason
{'id': 1} | No | Strings and names must use double quotes
{"id": 1,} | No | Trailing commas are not part of the grammar
{"id": 1} // note | No | The grammar has no comment syntax
{"ratio": NaN} | No | NaN and Infinity are not number values
{"code": 007} | No | Leading zeros are not allowed
{"delta": +1} | No | A leading plus sign is not allowed
{"x": .5} | No | A digit is required before the decimal point
"just a string" | Yes | Any value may be the top-level value
{"a": 1, "a": 2} | Yes, syntactically | Names should be unique; how receivers handle duplicates is unpredictable

The last row shows the difference between syntax validation and interoperability. A syntax checker can accept a document that different receivers will still read differently. If duplicates matter to you, use a validator that flags them or a profile such as I-JSON.

Is formatting or re-serializing suitable for your use?

For most configuration files, API payloads and logs, pretty-printing or minifying is safe and useful. Indentation makes nesting easy to review, and minification shrinks payloads. The deciding tradeoff is how the tool works. A tool that only adds or removes whitespace between tokens preserves meaning. A tool that parses the data into native values and writes it back out can silently change it. Treat JSON as exact text instead when the document carries large integer IDs, high-precision decimals, deliberate duplicate names or a cryptographic signature or hash computed over its exact bytes. In those cases, validate without rewriting, or use a whitespace-only formatter.

What formatting and validation actually change

Validation alone changes nothing. It reads the text and reports whether it matches the grammar. Pretty-printing and minifying change the stored characters, but only the insignificant whitespace between tokens. Whitespace inside strings is data and must stay untouched. Syntax highlighting, code folding and line numbers in an editor or viewer are display features. They do not alter the file, so what you copy out of a highlighted view should be the plain text underneath.

Problems start when a formatter round-trips the data through a parser. Take a parser that stores numbers as IEEE 754 double-precision values, as JavaScript's JSON.parse does. If its result is written back out with JSON.stringify, this input:

{"id": 9007199254740993, "price": 1.10, "city": "caf\u00e9", "a": 1, "a": 2}

becomes this output:

{"id":9007199254740992,"price":1.1,"city":"café","a":2}
  • The ID changed. 9007199254740993 is 2 to the power of 53 plus 1, and it cannot be represented exactly as a double, so it rounds to a neighboring value. RFC 8259 notes that only integers from -(2^53)+1 to (2^53)-1 are broadly interoperable for this reason.
  • The price lost its trailing zero. The number has the same value, but the text differs. That matters if the exact digits carry meaning, such as a currency scale.
  • The escape sequence \u00e9 was replaced by the literal character é. The value is identical, but byte-level comparisons and signatures will no longer match.
  • One of the duplicate names disappeared. This parser keeps the last value; other parsers may keep the first value or raise an error.
  • Other implementations can also reorder keys, change exponent notation or reject lone surrogate escapes. Check your parser's documentation rather than assuming it behaves like this example.

Will the result stay valid after copying or sharing?

Most breakage happens after the file leaves the editor. Software along the way can change characters that JSON treats strictly.

  • Typographic substitution: word processors and other editors that convert straight quotes to curly quotes produce text no JSON parser will accept.
  • Invisible characters: a non-breaking space or other Unicode space between tokens is not permitted whitespace, even though it looks identical to a regular space.
  • Byte order marks: some editors add one when saving UTF-8. The RFC lets parsers ignore it but does not require them to, so some will reject the file.
  • Line wrapping: a tool that inserts a real line break inside a long string makes the document invalid, because raw control characters are not allowed in strings.
  • Non-UTF-8 encodings: text saved in a legacy encoding can arrive with damaged non-ASCII characters even when the syntax still parses.

To share JSON reliably, send it as a file or in a plain-text or code block, then validate it again at the destination before using it.

Readability depends on the audience. Pretty-printed JSON is easier for people to review, compare in version control and search line by line. Minified JSON puts everything on one line, which makes line-based diffs and searches far less useful. For readers who are not developers, raw JSON of either kind is a poor substitute for a plain-language summary of what the data says. The same applies to documentation or support messages that need to work with assistive technology.

Gaps in this review

No specific validator, formatter, editor, messaging service or screen reader was tested for this article. The behavior described above comes from the published standards and from the documented behavior of JavaScript's built-in JSON functions. How a given screen reader announces braces, quotes and indentation depends on its punctuation settings and was not verified. Before relying on a tool, check its documentation or run a short test with your own data. Useful cases to test are a large integer, a duplicate name, a byte order mark and a non-ASCII escape.