Pretty-print JSON without breaking it
A practical guide to formatting, validating, and minifying JSON — with pitfalls that trip up APIs and config files. Use the live JSON Formatter when you want the same steps in one click.
Why pretty-print at all?
Production payloads are often minified: one line, no spaces, hard to read in a log. Pretty-printing rewrites a valid JSON value with indentation so humans can scan keys and nested arrays. Minifying does the reverse for smaller request bodies. Both operations should parse first, then serialize. Formatting characters alone (inserting newlines by guesswork) can invent invalid documents.
The safe sequence
- Treat the text as data, not as a string you “clean up” by hand.
- Parse with a strict JSON parser (
JSON.parsein the browser, or your language’s equivalent). - On success, serialize with a fixed indent (Fieldkit uses 2 spaces) or with no whitespace for minify.
- On failure, fix the syntax at the reported position — do not ship the broken text.
Fieldkit’s JSON Formatter & Validator follows that sequence in your tab: Format and Minify both parse, then stringify.
Worked examples
Minified API response. Start with:
{"ok":true,"user":{"id":42,"roles":["admin","editor"]}}
After a proper pretty-print you should see nested objects and arrays on their own lines, with consistent indentation. The values must stay the same: true stays boolean, 42 stays a number, strings keep their contents.
Config headed to a request body. If you edited a multi-line document and need the smallest legal form, minify after parse. Size drops because whitespace is removed; meaning must not change.
Invalid trailing comma. This is legal in JavaScript object literals but not in JSON:
{"name": "Fieldkit",}
A strict parser rejects it. Remove the comma after the last property, then format again. The same class of errors includes single-quoted strings, unquoted keys, and // comments.
Common pitfalls
- JSONC / JSON5 assumptions. Editor configs sometimes allow comments. Wire formats and many APIs do not. Strip comments before parse, or use a dedicated JSONC tool.
- Duplicate keys. Parsers typically keep the last value for a repeated key. Pretty-printing will not warn you that an earlier key disappeared.
- Large integers. IEEE-754 doubles cannot represent every integer above 253−1 exactly. IDs from some systems should travel as strings if exact digits matter.
- Two values in one paste.
{}{}is not one JSON value. Wrap in an array or send one document at a time. - Number spelling.
1.0often becomes1after round-trip. That is normal for JSON numbers.
When to use Fieldkit vs your editor
Use the browser tool when a log line or webhook lands in chat and you need a quick read. Use your editor or CI when the file lives in a repo, needs a formatter on save, or must match a project’s lint rules. For spreadsheet export of arrays of objects, see the sibling JSON → CSV tool instead of hand-copying tables.
Open the live tool
Open JSON Formatter — paste, Format or Minify, copy the result. Related reading: FAQ · Cron expressions guide.