Developer
JSON Formatting vs JSON Validation
Pretty-printed JSON and valid JSON are two different claims — one is about how it looks, the other about whether it's correct.
Published by UseKitl · Last reviewed 7 September 2026
"Format my JSON" and "validate my JSON" sound like the same request, and a lot of tools blur them together. They aren't the same operation. Formatting changes whitespace and indentation so a human can read the structure. Validation checks whether the content actually satisfies the JSON grammar — and a document can be perfectly, beautifully formatted while still being invalid.
What formatting does (and doesn't) do
Formatting — sometimes called "beautifying" or "pretty-printing" — adds consistent indentation and line breaks so nested objects and arrays are visually legible. It is purely cosmetic: it does not check correctness, and it does not change what the data means. The inverse operation, minifying, strips all non-essential whitespace to reduce the byte size for transport — useful for an API response, useless for a human trying to read the structure.
What validation actually checks
Validation parses the text against the JSON grammar defined in RFC 8259 and reports whether it's structurally correct — matched braces and brackets, correctly quoted keys and strings, no trailing commas, numbers in a valid format, and so on. A validator that only says "invalid" is only half useful; a useful one reports where the error is, so the fix doesn't require re-reading the entire document character by character.
Common JSON errors, and what actually causes them
| Symptom | Actual cause |
|---|---|
| Trailing comma after the last item | JSON, unlike JavaScript object literals, does not permit a trailing comma before } or ] |
| Unquoted or single-quoted keys | Object keys must be double-quoted strings — JavaScript's relaxed object-literal syntax is not valid JSON |
| A stray comment | JSON has no comment syntax at all — // and /* */ are both invalid, unlike JavaScript or JSON5 |
| NaN, undefined, or a bare function | Only strings, numbers, booleans, null, objects and arrays are valid JSON values |
| A trailing or leading data fragment | Usually an accidental paste of two JSON documents concatenated, or a stray character outside the outer braces |
Most of these come from the same root cause: JSON looks almost identical to a JavaScript object literal, but is a stricter subset of it. Code that happens to work when pasted into a JavaScript console can still be invalid JSON.
Why treating them as the same thing hides bugs
A formatter that silently "fixes" small syntax issues while re-indenting the document can produce output that looks correct and even parses locally, while masking the fact that the original input was invalid — which matters if that same input is about to be sent to a system with a stricter parser. Format and validate are best treated as two explicit steps: validate first so any error is caught and located, then format the confirmed-valid result for readability. Reversing the order, or collapsing them into one silent operation, is how a malformed payload reaches production looking clean.
When to minify instead
Minified JSON is for transport, not for people — an API response, a configuration value embedded in a URL, or any payload where every byte counts. It is functionally identical to the formatted version (same data, same validity) with all optional whitespace removed. Keep a formatted copy for development and debugging, and minify only at the point the data is actually sent.
A practical workflow
- Paste the raw JSON in.
- Validate first. If it's invalid, fix the reported position before doing anything else — formatting invalid JSON just produces prettier invalid JSON.
- Format the validated result for review, or to hand to someone reading it.
- Minify only the final version you actually intend to transmit or store, not the version you're still editing.
Limitations
- Validity is not the same as correctness for your application — a document can be syntactically valid JSON while still having the wrong shape, missing required fields or the wrong data types for what a consuming system expects. That requires a schema check, not just a syntax check.
- JSON has no native support for comments, dates, or trailing commas — code that generates JSON5 or JSONC (both common in config files) will fail strict JSON validation by design, not by error.
Sources
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format — the formal grammar that defines what counts as valid JSON
Found an error or something outdated in this guide? Report a correction.