Toolwise
Developer Tools

JSON Formatting, Validation and Minifying Explained

By the GrainZap Team8 min read
jsonjson formatterjson validatorapideveloper tools
Advertisement

JSON is the default language of APIs, config files, logs and browser storage. It's popular because it's simple: objects, arrays, strings, numbers, booleans and null, and nothing else. That simplicity is also why it's strict. A single misplaced comma anywhere in a 5,000-line payload makes the whole document invalid, and the parser on the other end will refuse all of it rather than guess what you meant.

Most JSON tools bundle three separate jobs together: formatting (making JSON readable), validating (checking that it's legal JSON at all) and minifying (making it as small as possible). They're related, but you reach for each one at a different moment, and it helps to know which problem you're actually solving.

Formatting: making JSON readable

Formatting — also called beautifying or pretty-printing — adds line breaks and indentation so the structure of the data is visible. It doesn't change the data at all. A formatted document and its single-line original parse to exactly the same values; only the whitespace between tokens is different.

This matters because JSON coming off the wire is usually one enormous line. An API response with nested customer, order and line-item objects is effectively unreadable in that form. Once it's indented, you can see at a glance which fields belong to which object, where an array ends, and whether a value you expected is actually missing or just nested one level deeper than you thought.

  • Debugging an API response that doesn't contain what your code expects.
  • Reviewing a config file before committing it, so diffs are line-by-line rather than one giant changed line.
  • Reading a log entry or webhook payload that was stored as compact JSON.
  • Sharing a sample payload in documentation or a bug report, where a human needs to read it.

Two-space indentation is the most common convention, and it's what JSON.stringify(value, null, 2) produces in JavaScript. Four spaces or tabs are equally valid — the choice is purely cosmetic, so match whatever your team or codebase already uses.

Validation: is this actually JSON?

Validation answers a yes-or-no question: does this text follow the JSON grammar defined in RFC 8259? A validator parses the input and either succeeds or stops at the first thing it can't understand. Formatting a document implicitly validates it, because a formatter has to parse the JSON before it can re-indent it — which is why a formatter that refuses your input is really telling you it's invalid.

The most useful time to validate is before JSON leaves your hands: before you paste it into an API client, commit a config file, or hand a payload to another team. A receiving server that gets malformed JSON will usually return a generic 400 Bad Request, which tells you far less than a validator pointing at the exact character that broke.

Strict JSON is not a JavaScript object literal

The most common source of invalid JSON is text that looks like JSON but was written as a JavaScript object. JavaScript is far more forgiving — it accepts trailing commas, unquoted keys, single-quoted strings and comments. JSON accepts none of them. Code copied out of a .js file, or hand-edited by someone used to writing JavaScript, fails validation for exactly this reason.

Reading error messages with line numbers

In a small payload you can spot a stray comma by eye. In a large one you can't, which is where position information earns its keep. Modern parsers report where they gave up — for example, "Expected property name or '}' in JSON at position 1 (line 1 column 2)". A validator that shows the line and column lets you jump straight to the problem instead of scrolling through thousands of lines.

Tip

The reported position is where the parser noticed the problem, not always where you made the mistake. A missing closing quote or bracket is often reported a line or two later, at the first character that no longer makes sense. If the flagged line looks fine, check the line immediately above it.

The syntax mistakes behind most invalid JSON

Nearly every "invalid JSON" error in practice comes from a short list of rules. Here they are, with the broken form and the fix:

  • Trailing commas: {"a": 1, "b": 2,} is invalid. The last item in an object or array must not be followed by a comma — remove it to get {"a": 1, "b": 2}.
  • Unquoted keys: {name: "Asha"} is valid JavaScript but not JSON. Every key must be a double-quoted string: {"name": "Asha"}.
  • Single quotes: {'name': 'Asha'} is invalid. JSON strings, including keys, must use double quotes only.
  • undefined: {"email": undefined} is invalid, because undefined doesn't exist in JSON. Use null to represent a missing value, or leave the key out entirely.
  • Comments: // and /* */ comments are not part of JSON and will fail strict parsing. If a config needs annotations, use a format that supports them (such as JSONC or YAML) or a dedicated "_comment" field.
  • Other non-JSON values: NaN, Infinity, and hexadecimal numbers like 0x1F are not valid JSON numbers, and leading zeros such as 007 are rejected too.

A related trap runs the other way. When JavaScript serialises an object with JSON.stringify, it silently drops properties whose value is undefined and turns NaN into null. So {a: undefined, b: 1} becomes {"b":1}. The output is valid JSON, but a field has quietly vanished — worth knowing when an API reports a "missing" property you were sure you sent.

Watch out

Some tools, including certain editors' settings files, accept "JSON with comments" and trailing commas. That's a convenience extension, not JSON. Anything you send to an API or parse with a standard library should be validated as strict JSON, because the parser on the other end almost certainly is strict.

Minifying: making JSON small

Minifying is formatting in reverse. It strips every space, tab and line break that sits between tokens, leaving the smallest valid representation of the same data. Whitespace inside string values is untouched, since that's part of the data itself.

The savings depend on how deeply nested the data is, because indentation grows with depth. As a worked example, take an API response containing 50 orders, each with a nested customer object and a line-items array:

  • Pretty-printed with 2-space indentation: about 13,500 bytes.
  • Minified: about 7,000 bytes — roughly half the size.
  • Pretty-printed, then gzip-compressed: about 760 bytes.
  • Minified, then gzip-compressed: about 670 bytes — a gap of around 12%.

That last comparison is the realistic one. Most production servers gzip or Brotli-compress responses, and compression is very good at squeezing out repeated whitespace. Minifying still helps — smaller payloads parse slightly faster and cost less when compression isn't available — but the dramatic 50% figure shrinks considerably once compression is in the picture.

Which one to use, and when

  • Reading or debugging: format it. Structure you can see is structure you can reason about.
  • Before sending or committing: validate it. Catch the trailing comma now, not in a 400 error later.
  • Production payloads, embedded data and storage: minify it. Nobody reads these by hand, so whitespace is pure overhead.
  • Config files in version control: keep them formatted, so each change shows up as a clear, reviewable diff.

Frequently asked questions

Does formatting or minifying change my data?

No. Both only change whitespace between tokens, never keys, values or order. Parsing the formatted version and the minified version gives identical results. If a tool changes a value — rounding a very large number, for instance — that's a parsing limitation of the tool, not a property of formatting.

Is it safe to paste production JSON into an online formatter?

It depends on where the processing happens. A formatter that runs entirely in your browser never sends the data anywhere, but one that posts it to a server does. For payloads containing tokens, customer data or credentials, use a client-side tool or redact the sensitive fields first.

The short version: format JSON when a human needs to read it, validate it before a machine needs to accept it, and minify it when neither applies. Learn the five or six strictness rules — double quotes, no trailing commas, no comments, null instead of undefined — and most JSON errors stop happening in the first place.

Advertisement

Ready to put this into practice?

Format Your JSON Free

More guides