JSON guide · 4 min read
How to Format and Pretty-Print JSON
Pretty-print compact JSON safely, choose indentation, understand serialization side effects and format payloads in browser or code.
Formatting changes whitespace, not the intended data model
Pretty-printing parses JSON and writes it again with line breaks and indentation. The goal is readability: nested objects become visually obvious, arrays are easier to scan and code review becomes less error-prone.
Minification performs the opposite transformation by removing unnecessary whitespace. A valid formatted document and its valid minified form represent the same JSON value, even though their byte-for-byte text differs.
Minified:
{"user":{"id":42,"roles":["admin","editor"]}}
Pretty-printed:
{
"user": {
"id": 42,
"roles": [
"admin",
"editor"
]
}
}Validate before assuming a formatter can repair the input
A normal formatter needs to parse the document before it can reserialize it. If the input has a trailing comma, broken quote or missing brace, formatting should fail rather than silently inventing an interpretation.
Use the JSON Syntax Validator when formatting fails. If you intentionally want a best-effort repair, use a repair tool separately and review the resulting document before formatting it.
Choose indentation for the reader and environment
Two spaces is common for JSON in web projects, while four spaces can be easier to scan in deeply nested configuration. Tabs are also possible when a team standard requires them. The specific width is less important than consistency within a file and repository.
For API responses transferred over the network, HTTP compression usually saves far more bytes than hand-minifying a small payload. For files committed to source control, readable formatting can be more valuable than a few kilobytes of whitespace.
Format JSON in the browser without uploading it
Paste the JSON into JSON Formatter & Minifier, validate or format it, then copy or download the output. Browser-local processing is useful for configuration, logs and payloads that should not be sent to an application server simply to change whitespace.
For very large documents, a full parse can still consume significant browser memory even when no upload occurs. A structure-oriented viewer may be more appropriate than rendering every line of a multi-hundred-megabyte file.
Format JSON with JavaScript
JSON.parse converts source text into a JavaScript value. JSON.stringify serializes that value back to JSON; its third argument controls indentation. Wrap parsing in normal application error handling when the source is untrusted or user-supplied.
const value = JSON.parse(rawJson);
const twoSpaces = JSON.stringify(value, null, 2);
const fourSpaces = JSON.stringify(value, null, 4);
const minified = JSON.stringify(value);Format JSON with Python or jq
For repeatable scripts or CI pipelines, command-line formatting is often more convenient than a browser. Python's json module and jq both parse before writing output, which means they also act as basic syntax checks.
# Python
python -m json.tool input.json > formatted.json
# jq
jq . input.json > formatted.json
# jq minified
jq -c . input.json > minified.jsonUnderstand parse-and-serialize representation changes
Formatting is conceptually a whitespace operation, but most formatters implement it by parsing and serializing. That process can normalize escape sequences, numeric spelling or key presentation even when the underlying value remains equivalent.
JavaScript numbers are IEEE-754 doubles, so integers beyond the safe-integer range can lose precision if they are parsed as numbers. If an identifier must preserve every digit across systems, represent it as a string or use a parser designed for arbitrary-precision numbers.
// Risky when every digit matters in JavaScript
{"accountId": 9007199254740993}
// Safer cross-system identifier representation
{"accountId": "9007199254740993"}Use formatting as part of a review workflow
For production configuration or a payload generated by another system, formatting should make review easier rather than replace validation. After formatting, inspect the branches that matter, run schema validation when available and compare the output with the source when representation changes could be significant.
- Keep the original when the payload is evidence for a bug.
- Validate syntax before relying on the formatted output.
- Use consistent indentation in source-controlled files.
- Watch large integers, escape sequences and application-specific ordering requirements.
- Minify only when compact representation has an actual operational benefit.
Primary sources
References and specifications
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format
The IETF specification for JSON syntax, values, interoperability and parser behavior.
- ECMA-404 — The JSON Data Interchange Syntax
The concise ECMA definition of JSON grammar.
Common questions
Frequently asked questions
Does formatting JSON change its values?
A correct formatter is intended to preserve the JSON value while changing whitespace. Because many tools parse and serialize, textual representations such as escapes or very large numbers can still require review.
Should production JSON always be minified?
No. Minification can reduce raw size, but HTTP compression often provides a larger benefit. Human-readable formatting is frequently better for configuration, fixtures and debugging artifacts.
Why won't a formatter accept my JSON?
The input is probably syntactically invalid or incomplete. Validate the raw text first and fix errors such as trailing commas, missing quotes or unmatched delimiters.
What indentation should I use?
Two or four spaces are both reasonable. Follow the convention of the repository or team and prioritize consistency over a universal preference.