How to Pretty Print JSON: Formatting Guide
Learn how to beautify JSON with indentation, choose spacing styles, and when to minify instead.
What Pretty Printing Means
Pretty printing JSON means adding whitespace — indentation and line breaks — to make a JSON document human-readable without changing its parsed value. A minified JSON object like `{"name":"Alice","age":30}` becomes a multi-line, indented structure that developers can scan quickly. The term comes from the broader concept of pretty printing in programming, where formatters beautify code for display.
Pretty printing is a development and debugging activity, not a production requirement. APIs transmit minified JSON to save bandwidth. Developers pretty print to understand structure, find errors, and review data during code reviews. The parsed result is identical regardless of formatting — whitespace is not semantically significant in JSON.
Format Hubs's JSON Formatter pretty prints JSON instantly in your browser. Choose indentation of 2, 3, or 4 spaces, or use tab characters. Paste minified API responses, log entries, or config files and click Format to beautify them.
How to Pretty Print JSON Online
The fastest method: paste your JSON into the JSON Formatter input editor and click the Format button. The output panel displays beautified JSON with your chosen indentation. Copy the result to your clipboard or download it as a `.json` file.
If your JSON might contain syntax errors, validate first with the JSON Validator. The formatter requires valid JSON to produce output — invalid input shows an error with the line and column of the problem. Fix errors, then format.
For interactive exploration rather than flat text, use the JSON Viewer. It renders JSON as a collapsible tree where you can expand nodes, search for keys and values, and edit JSON with live tree updates. Pretty printing gives you formatted text; the tree viewer gives you navigable structure.
Pretty Printing in Code
JavaScript provides `JSON.stringify(value, null, 2)` where the third argument is the indentation level. Use `2` or `4` for spaces, or `"\t"` for tabs. Node.js developers often pipe objects through this for debug logging. Python's `json.dumps(obj, indent=2)` achieves the same result.
In Go, use `json.MarshalIndent(v, "", " ")`. In Java, Jackson's `ObjectMapper` with `writerWithDefaultPrettyPrinter()` formats output. Every language's JSON library includes a pretty print option — check your standard library before reaching for a third-party formatter.
Avoid pretty printing in production logs at high volume — indented JSON consumes significantly more storage and bandwidth. Pretty print on demand during debugging, and log minified JSON in production. Use the JSON Minifier to compress formatted JSON back to a single line when needed.
Choosing Indentation Style
Two-space indentation is the most common convention for JSON files in web projects. It balances readability with compactness. Four-space indentation is popular in enterprise codebases and matches many coding style guides. Tab indentation saves characters but can render inconsistently across editors.
Consistency matters more than the specific choice. If your project's `package.json` uses 2-space indentation, format all JSON config files the same way. Mixed indentation in a repository creates noisy diffs and confuses reviewers.
The JSON Formatter lets you switch indentation on the fly. Format the same document with 2, 3, or 4 spaces to compare readability, then pick the style that fits your team's conventions.
Pretty Print vs Minify
Pretty printing and minifying are inverse operations. Pretty printing adds whitespace for readability. Minifying removes all non-essential whitespace to reduce file size. Round-tripping between the two is lossless — minify a pretty-printed file and you get the same result as minifying the original.
Use pretty printing during development, debugging, and documentation. Use minification for API responses in production, asset bundling, and anywhere bandwidth matters. The JSON Minifier compresses JSON to a single line; the JSON Formatter expands it back.
Typical size reduction from minification is 20-40% depending on how heavily the original was indented. For a 10 KB config file, that might save only a few kilobytes. For an API serving millions of requests per day, the savings compound significantly.
Common Pretty Print Scenarios
Debugging API responses: copy the response body from browser DevTools or Postman, paste into the JSON Formatter, and scan the indented output for the fields you need. This is faster than reading a single-line response.
Reviewing configuration changes: when a teammate modifies a JSON config in a pull request, pretty printed diffs show exactly which nested values changed. Many Git platforms auto-format JSON diffs, but pre-formatting ensures clarity.
Preparing documentation examples: technical writers and developers include formatted JSON in README files, API docs, and blog posts. Consistent indentation makes examples professional and easy to follow. Validate examples with the JSON Validator before publishing.
Troubleshooting Formatting Issues
If the formatter reports an error, your JSON has a syntax problem — not a formatting problem. Common causes include trailing commas, single quotes, unquoted keys, and comments. Fix syntax first, then format. See How to Validate JSON for a complete error catalog.
Very large JSON files (tens of megabytes) may slow browser-based formatters due to memory limits. For huge files, use command-line tools: `python -m json.tool input.json` or `jq . input.json`. Browser tools handle most practical development file sizes comfortably.
If formatted output looks wrong — fields in unexpected order, missing data — the issue is likely in the data itself, not the formatter. JSON object key order is not semantically significant, but some formatters preserve insertion order. Use the JSON Viewer to verify the structure matches your expectations.
Related Tools
Format JSON online for free. Beautify, minify, and validate JSON instantly in your browser — private, fast, and no sign-up required.
Explore JSON as an interactive collapsible tree with search. Free online JSON tree viewer and explorer.
Compress JSON by removing whitespace and line breaks. Free online JSON minifier tool.
Validate JSON syntax online with precise line and column error messages. Free, fast, and private.
Related Articles
What is JSON? Definition, Syntax, and Examples
Understand what JSON is, how it works, and why it became the default format for APIs and configuration.
How to Validate JSON: Tools and Techniques
Step-by-step guide to validating JSON syntax, catching errors, and fixing common mistakes.
How to Parse JSON: A Complete Developer Guide
Learn how to parse JSON in JavaScript, Python, and other languages. Includes examples, common errors, and best practices.