The Complete Guide to JSON Formatting
JSON is the lingua franca of modern APIs, but a single stray comma can break an entire deploy. This guide walks through the most common formatting tasks and the pitfalls that trip up even experienced engineers.
What is a JSON formatter?
A JSON formatter takes raw JSON and re-serializes it with consistent indentation, predictable key ordering, and (optionally) sorted keys. The opposite operation is minification — stripping every byte of whitespace so the payload is as small as possible on the wire.
Most formatters support three output modes:
- Pretty — 2- or 4-space indentation, one key per line
- Minified — no whitespace, suitable for production
- Escaped — wrapped as a JavaScript string (double quotes escaped)
Common gotchas
- Trailing commas — Valid in JavaScript, invalid in strict JSON. Use a JSON fixer if your input came from a JS literal.
- Single quotes — JSON requires double quotes around keys and strings.
- Comments — Strict JSON doesn't allow
//or/ /. If you need comments, use JSON5 or JSONC. - Unescaped newlines inside strings — Every newline inside a string must be written as
\n.
When to minify
Always minify JSON that travels over the network: API responses, localStorage payloads, URL params. The readability win from pretty-printing only matters during debugging — strip it in production and your payloads shrink by 15-30%.
Related tools
- JSON Formatter — pretty-print and validate
- JSON Parser — inspect nested structures interactively
- JSON Fixer — repair malformed JSON before parsing