DevToolbox

JSON Formatting and Validation Explained (Pretty-Print, Minify, Validate)

By Editorial team · 2026-06-14

In short: Formatting JSON re-serializes already-valid data with consistent whitespace — pretty-printed for humans or minified for the wire — without changing its meaning. Validation is a separate step that checks the text parses as legal JSON at all. Most 'invalid JSON' errors come from trailing commas, single quotes, comments, or unquoted keys, none of which standard JSON allows.

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:

OperationWhitespaceData changed?Use it for
Pretty-printAdded (2- or 4-space indent)NoReading, debugging, diffs
MinifyRemovedNoAPI responses, caching, bandwidth
ValidateUntouchedNoConfirming 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:

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:

MistakeInvalidValid
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:

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

  1. Validate first. Paste the raw text into the JSON formatter and validator. If it reports an error, note the line and column.
  2. Scan for the usual suspects — trailing comma, single quotes, an unquoted key, or a stray comment.
  3. Pretty-print once it is valid so the nesting is obvious.
  4. 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.
  5. 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

Frequently asked questions

What is the difference between formatting and validating JSON?

Validation checks whether the text is legal JSON that a parser will accept. Formatting takes valid JSON and re-prints it with chosen whitespace — indented (pretty) or stripped (minified). You must be able to parse before you can format, so validation always happens first.

Why is my JSON invalid because of a trailing comma?

Standard JSON (RFC 8259) does not permit a comma after the last element of an array or object. JSON5 and JSONC allow it, but strict parsers like JSON.parse reject it. Remove the final comma or convert from JSON5 first.

Does minifying JSON change its data?

No. Minifying only removes insignificant whitespace between tokens. The keys, values, types, and ordering of array elements are identical, so the parsed result is exactly the same — just smaller on the wire.

Can JSON keys be unquoted or use single quotes?

Not in standard JSON. Every key and every string value must be wrapped in double quotes. Single quotes and unquoted keys are JavaScript object-literal syntax, not JSON, and will fail validation.

Related articles

Last updated: 2026-06-14