How to Format JSON: Pretty Print and Read JSON Easily
Formatting turns a wall of JSON into something you can actually scan. Here's what it does and how to do it.
JSON doesn't care about whitespace - {"user":{"id":42,"name":"Alice"}} and a nicely indented version of the same object are functionally identical. But one of them is scannable and one isn't. Formatting (also called pretty-printing) is purely a readability transformation, and it's one of the most-used steps in working with JSON day to day.
What formatting actually does
A formatter parses the JSON text into its underlying structure, then re-serializes it with consistent indentation - typically 2 or 4 spaces per nesting level - and a line break after each key-value pair or array item.
{"user":{"id":42,"name":"Alice","roles":["admin","editor"]}}becomes:
{
"user": {
"id": 42,
"name": "Alice",
"roles": ["admin", "editor"]
}
}Why indentation matters more as nesting grows
For a flat object with two or three fields, formatting barely matters - you can read it either way. The value shows up once you're dealing with a real API response: nested objects several levels deep, arrays of objects, optional fields that may or may not be present. Indentation is what lets your eye track which closing brace belongs to which opening one.
Formatting programmatically
If you already have a parsed JavaScript object rather than raw text, formatting is a built-in one-liner:
JSON.stringify(value, null, 2); // 2-space indent
JSON.stringify(value, null, "\t"); // tab indentThe third argument controls the indentation - a number for that many spaces, or a string (like a tab character) to use literally.
Formatting pasted or malformed JSON
The more common scenario is you have raw JSON text - copied from an API response, a log line, or a config file - and it's either minified or just inconsistently formatted. In that case you need a tool that parses the text first, which also means it'll immediately tell you if something's syntactically wrong before it can format it.
The JSON Formatter handles exactly this: paste minified or messy JSON and it validates, then formats it with your choice of 2-space, 4-space, or tab indentation - along with a tree view for navigating deeply nested structures.
2 spaces vs. 4 spaces vs. tabs
| Style | Common in | Trade-off |
|---|---|---|
| 2 spaces | JavaScript/JSON ecosystems, most style guides | Compact, fits more nesting on screen |
| 4 spaces | Python-influenced teams, some enterprise style guides | More visually distinct per level, uses more horizontal space |
| Tabs | Editor-configurable indentation, accessibility preferences | Width adjusts per viewer's editor settings |
None of these is objectively correct - pick whatever your team's existing style already uses, and be consistent.
When you'd want the opposite: minifying
Formatting is for humans reading the JSON. When JSON is headed over the network as an API payload or into storage, the opposite operation - minifying, stripping all whitespace - reduces payload size. See JSON Pretty Print vs Minify: What's the Difference? for when each makes sense.
Common mistakes
- Trying to format JSON that has a trailing comma or other syntax error - fix the syntax first (see Common JSON Syntax Errors and How to Fix Them).
- Manually re-indenting large JSON by hand instead of using a formatter, which is slow and easy to get wrong on deeply nested structures.
- Committing minified JSON to a config file in a repository, making diffs unreadable for anyone reviewing a small change.
Summary
Formatting JSON is a whitespace-only transformation that turns dense, single-line JSON into something readable by adding consistent indentation and line breaks. It doesn't change the data - it changes how easily a human can scan it, which matters a lot once you're past a few fields of nesting.
Frequently asked questions
- How do I format JSON?
- Paste the JSON into a formatter and it will re-indent it with consistent spacing and line breaks based on its nested structure - you can also do this manually with JSON.stringify(value, null, 2) in JavaScript if you already have the parsed object.
- Does formatting JSON change its data?
- No. Formatting only affects whitespace - indentation and line breaks. The keys, values, and structure remain exactly the same before and after.
- Can I format invalid JSON?
- No - formatting requires successfully parsing the JSON first. If it's invalid, you'll need to fix the syntax error before it can be indented.
Try it yourself
The tool that pairs with this guide.
Did this help you untangle that JSON?
If these free developer tools save you time, consider buying me a byte. Your support helps keep DevCraftKit free and growing.
Related articles
JSON Validator: How to Find and Fix Invalid JSON
A single misplaced comma breaks the whole document. Here's how validation finds it and what the common causes are.
Published
JSON Pretty Print vs Minify: What's the Difference?
Same data, two shapes - one for humans, one for the network. Here's when each one is the right choice.
Published
Common JSON Syntax Errors and How to Fix Them
Nine mistakes cause almost every "invalid JSON" error you'll ever see. Here's each one, with the fix.
Published