How to pretty print JSON
June 7, 2026 · 12 min read
Minified JSON saves bytes on the wire but punishes humans during incidents. Pretty printing restores newlines and indentation so you can scan nesting depth, spot missing commas, and compare sibling keys quickly. The skill is knowing when to format locally without changing what production systems store or hash.
Pretty printing is not a substitute for validation. Always parse first, then format. A formatter that “fixes” invalid JSON by guessing can hide the root cause of a partner integration failure.
Why pretty printing matters
Support and engineering workflows rely on readable payloads: webhook replays, CloudWatch log excerpts, and HAR exports from browsers. Readable JSON reduces mean time to resolution because paths and types are visible without horizontal scrolling through a single line.
In code reviews, pretty diffs on normalized JSON make intent obvious. Pair formatting with sorted keys when snapshots must be stable across operating systems and serializer versions.
Indentation and width
Two-space indentation is the de facto standard in web tooling; four spaces appear in enterprise Java stacks. Pick one per repository and enforce it in formatters. Line wrapping is optional - some teams cap width at 100 columns for side-by-side review in terminals.
Compact arrays of numbers or hashes sometimes stay on one line by custom formatters; default pretty printers expand everything. For giant arrays, consider preview tools that collapse siblings after a depth threshold.
const obj = { id: 1, meta: { role: "admin", active: true } };
// Pretty (2 spaces) - note: key order follows insertion in JS
console.log(JSON.stringify(obj, null, 2));
Sorting keys
Sorted keys make pretty output deterministic, which stabilizes Git diffs and checksums. Sort recursively when generating canonical forms for signatures; sort only top-level when inner order carries meaning (ordered maps).
After sorting, pretty print once. Doing the reverse can leave teams confused about whether a diff shows semantic or cosmetic changes.
Redacting secrets
Pretty printing increases the risk of pasting secrets into tickets because nested fields become visible. Redact tokens, cookies, and private keys before sharing formatted JSON in Slack or email. Use allow-lists of field names to scrub automatically in log pipelines.
Remember that formatting does not remove data - it only changes whitespace. Treat formatted files with the same classification as the original minified blob.
CLI and editor tips
jq with . pretty-prints valid JSON in the terminal. Editor commands “Format Document” should target JSON language mode, not JavaScript, to avoid accepting non-standard syntax.
Browser-based formatters help when corporate laptops block CLI installs. Prefer tools that run locally in the browser when payloads contain PII or unreleased product data.
FAQ
- Does pretty printing change JSON semantics?
- No. Whitespace between tokens is insignificant to parsers. Key order may change only if you sort keys as part of formatting.
- What indent size should I use?
- Two spaces is most common for web APIs. Consistency within a repo matters more than the exact number.
- Can I pretty print JSON with single quotes?
- Not as strict JSON. Convert to double-quoted strings first or fix the source to be valid JSON.
- Is pretty JSON safe to log?
- Formatting does not remove sensitive fields. Redact secrets before logging or sharing formatted output.