How JSON Formatting and Validation Work
What pretty-printing and minifying actually do to JSON, why a formatter doubles as a validator, and how to check JSON is correct.
JSON is a text format for structured data, and machines don't care how it's laid out. A formatter's job is to make that same data readable for humans — and, as a side effect, to prove the data is actually valid.
This guide explains what formatting does at the byte level and why 'formatting' and 'validating' are really the same operation.
Whitespace is optional in JSON
JSON's grammar treats spaces, tabs, and newlines between tokens as insignificant. These two documents mean exactly the same thing to a parser:
{"name":"Ada","skills":["math","code"]}
{
"name": "Ada",
"skills": [
"math",
"code"
]
}The first is minified (one line, no extra whitespace) — good for sending over a network because it's smaller. The second is pretty-printed with indentation — good for reading and editing. Formatting is just adding or removing that insignificant whitespace.
Indentation and depth
A pretty-printer indents each level of nesting by a fixed amount — commonly two or four spaces, or a tab — so you can see at a glance which braces and brackets belong together. Nested objects and arrays line up under their parents, which is what makes a large payload scannable.
Why a formatter is also a validator
To reformat JSON, a tool first has to parse it into a real data structure, then write it back out. If the input isn't valid JSON, parsing fails and there's nothing to reformat. That's why a formatter that round-trips through a genuine parser can tell you two things at once: here is your data laid out cleanly, and by the way, it is (or isn't) syntactically valid.
A tool that merely re-indents text with regular expressions can't do this — it might 'pretty-print' something that is actually broken. Real validation means the JSON grammar was accepted.
Strict JSON trips people up
Many of these are tolerated in JavaScript object literals but not in JSON itself, which is a stricter subset. If your JSON fails to parse, one of these is usually the cause.
- Keys and string values must use double quotes — single quotes are invalid.
- Trailing commas after the last item are invalid.
- Comments are not part of JSON and make it invalid.
- Values like undefined or NaN are not allowed (use null instead).

