How to Format JSON (Pretty-Print and Minify)
Formatting is not decoration. Indentation is for humans, minification is for transport, and failures mean you must fix syntax first. Pick the method that matches the job.
What does “format JSON” mean?
Pretty-print inserts newlines and indentation without changing meaning. Minify strips that whitespace. Both walk the same tree: object key order is not a business guarantee, but strings, numbers, and nesting must stay intact.
Network panels usually show minified responses to save bytes. Humans need the expanded tree. Issue trackers and some curl samples want a single line so shells do not split the body. Format and minify are opposite directions of one job—not two products.
If the text is not JSON, a formatter should refuse to guess. Single quotes, trailing commas, and comments are errors. Use the JSON validator for syntax. If you still need the data model, read what JSON is.
Why bother?
Three levels of nesting in one line hides data.user.profile next to unrelated keys. Two-space indent is the usual debugging default.
Minify when you paste into a ticket, an env var, or a request body. Extra whitespace changes byte length, not the parsed value. If validate-before and validate-after disagree, the text was edited—not “broken by minify”.
When comparing two payloads, format both (or minify both) before diffing, or you will review spaces. Our text diff is for snippets, not Git.
How to format JSON in practice
Fastest path in a browser: open the JSON formatter, paste, pretty-print, optionally minify. Invalid input should error rather than rewrite {id:1} into something else. Input stays on the device by default.
For files you already have, python -m json.tool, jq ., or the editor formatter is better. The online page exists for a string copied from chat that you do not want to save first.
On failure: stop retrying pretty-print, read the first syntax error, validate, then expand. A missing comma is a validator job, not a “stronger beautifier” job.
Pick a method, do not collect sites
Need the tree: pretty-print. Need a yes/no on grammar: validate. Need types: formatting is only the first step; then JSON to TypeScript or JSON to Go on the JSON hub.
Do not paste production secrets. Local processing still does not make a shared machine safe. Multi-megabyte dumps belong in jq, not a tab meant for API samples.
Search results named “complete methods” are usually more bookmarks. The useful trio is: read, validate, minify. That is the whole job.
Related tools
FAQ
Does formatting change values?
A correct implementation only changes whitespace. If numbers look different, check integer size and whether the original text was valid JSON.
Nothing happened when I clicked format.
It may already be indented, or parse failed. Read the message; do not switch to a site that “auto-fixes” by mutating data.
Two spaces or tabs?
Two spaces are the usual API convention. Tabs parse too, but they fight in diffs. Pick one.
Can I format a fragment?
Not as a document. Complete the root or paste a whole object.
Formatter vs validator?
Formatter is for reading and shipping. Validator answers “is this JSON?”. Validate first when it fails.