JSON (JavaScript Object Notation) is the default data-interchange format of the modern web. It looks simple, but two operations get conflated constantly: formatting and validation. They solve different problems, and knowing the difference is the fastest way to debug a broken API response. You can try both on real data with our JSON formatter and validator.
What does formatting JSON actually do?
Formatting re-serializes already-valid JSON with consistent whitespace. The data does not change — only its presentation does. There are two directions:
- Pretty-print: add indentation and line breaks so a human can scan the structure. Best for debugging, documentation, and committing config files.
- Minify: strip every byte of insignificant whitespace so the payload is as small as possible. Best for network responses and storage.
| Operation | Whitespace | Data changed? | Use it for |
|---|---|---|---|
| Pretty-print | Added (2- or 4-space indent) | No | Reading, debugging, diffs |
| Minify | Removed | No | API responses, caching, bandwidth |
| Validate | Untouched | No | Confirming the text parses |
Crucially, formatting requires the input to already be valid — you cannot pretty-print something a parser refuses to read. That is why validation comes first.
How does JSON validation work?
Validation checks that the text obeys the JSON grammar defined in RFC 8259. A strict parser such as JavaScript’s JSON.parse either returns a value or throws an error pointing near the first problem. Validation confirms the syntax is legal; it does not check that the data matches your expected shape — that is schema validation, a separate concern handled by tools like JSON Schema.
A valid JSON document is one of these values:
- An object:
{ "key": value, ... }with double-quoted string keys. - An array:
[ value, value, ... ]. - A string (double-quoted), number,
true,false, ornull.
What causes most “invalid JSON” errors?
The overwhelming majority of validation failures come from a handful of mistakes — usually from hand-editing or from pasting a JavaScript object literal instead of true JSON:
| Mistake | Invalid | Valid |
|---|---|---|
| Trailing comma | [1, 2, 3,] | [1, 2, 3] |
| Single quotes | {'a': 1} | {"a": 1} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comment | {"a": 1} // note | {"a": 1} |
undefined/NaN | {"a": undefined} | {"a": null} |
| Leading zero / hex | {"a": 0x1F} | {"a": 31} |
Note the recurring theme: JSON is a strict subset of JavaScript syntax, not the same thing. If you copied an object straight out of a .js file, expect trailing commas, comments, and single quotes to break validation. Supersets like JSON5 and JSONC deliberately allow those conveniences, but a standard parser will reject them.
When should you pretty-print versus minify?
A simple rule:
- Pretty-print when a human will read it: log output, config files in version control (smaller diffs), and anything you are debugging.
- Minify when a machine will read it: HTTP responses, cache entries, message-queue payloads, and embedded data where every byte counts.
Minification is lossless. Re-parsing a minified document yields the identical structure, so it is safe to minify on the way out and pretty-print on the way in.
A quick debugging workflow
- Validate first. Paste the raw text into the JSON formatter and validator. If it reports an error, note the line and column.
- Scan for the usual suspects — trailing comma, single quotes, an unquoted key, or a stray comment.
- Pretty-print once it is valid so the nesting is obvious.
- Inspect encoded fields. JSON often carries Base64 blobs or tokens. Decode those separately with our Base64 encode/decode tool, and remember that a JWT payload is just Base64URL-encoded JSON — see how to decode a JWT.
- Minify the final version before it goes over the wire.
Does formatting affect performance or correctness?
Pretty-printing increases byte size (more whitespace), which marginally increases transfer time over a network — hence minifying responses. But it has zero effect on correctness: the parsed object is identical either way. Object key order is preserved by most serializers but is not guaranteed to be semantically meaningful in JSON, so never rely on it. Array order, by contrast, is always significant and is preserved by formatting.
Sources
- IETF RFC 8259, The JavaScript Object Notation (JSON) Data Interchange Format.
- ECMA-404, The JSON Data Interchange Syntax.
- MDN Web Docs, JSON.parse() and JSON.stringify().