What the formatter does
It parses your JSON and writes it back with one value per line and consistent indentation. The data does not change. This is the layout you want for code review, documentation, a config file in git or a quick look at an API response.
Options
- Indent: 2 spaces (the common choice for JSON), 4 spaces, 8 spaces or a tab.
- Sort keys: orders the keys of every object A to Z, which makes two files easy to compare.
- Short arrays on one line: keeps lists of simple values, such as [1, 2, 3], on a single line instead of one number per line.
- Accept comments and trailing commas: for config files in JSONC style. They are removed, and the tool tells you.
Formatting errors
If the JSON is not valid, the tool shows the line and column of the first problem, with the line and a marker under the character. The usual causes are a trailing comma, single quotes, a missing quote around a key and a missing comma between items. See the JSON validator for the full list.
Why format JSON
- Review and version control. One value per line gives clean diffs in git, so a change shows up as a changed line, not a changed file.
- Consistency. Everyone on a team gets the same layout, whatever editor they use.
- Debugging. Structure errors are easier to see in indented text.
- Documentation. API examples are easier to follow when they are indented.
How sorting keys helps comparisons
Two JSON files with the same data can list their keys in different orders, and a text diff then shows many changes that are not real. Sort the keys of both files and the diff shows only what changed. For a comparison that ignores order, use the JSON diff tool.
What the formatter does not change
Values, key order (unless you sort), number text such as 1.50, and the text of every string stay as they are. The one rewrite is that an escape sequence inside a string may be written in its shortest form, for example \u00e9 becomes the letter é.
2 spaces, 4 spaces or a tab?
| Indent | Common in | Good for |
|---|---|---|
| 2 spaces | JavaScript and web projects, package.json | Deeply nested data that stays narrow |
| 4 spaces | Python json.tool, many Java and PHP teams | Easy scanning of shallow data |
| Tab | Teams that let each person set the tab width | Personal and accessibility preferences |
| 8 spaces | Rare | Very flat data, or teaching |
Short arrays on one line
Without the option, every array item gets its own line:
{
"size": [
1,
2,
3
]
}
With it, a list of simple values that fits in 80 characters stays on one line:
{
"size": [1, 2, 3]
}
Lists that contain objects or arrays are always expanded.
Format JSON in your editor and tools
- VS Code: open the file and run Format Document (Shift+Alt+F on Windows and Linux, Shift+Option+F on macOS).
- Prettier:
npx prettier --write data.json - Browser DevTools: the Network tab shows a formatted preview of JSON responses.
- PHP:
json_encode($data, JSON_PRETTY_PRINT)indents with 4 spaces.
Format, beautify, minify: what is the difference?
- Format and beautify both add line breaks and indentation. This page adds layout options, and the beautifier adds a colored view with line numbers.
- Minify does the opposite and removes all optional whitespace to save bytes.
On the command line
jq . data.json
jq -S . data.json # sort keys
python3 -m json.tool --indent 2 data.json
node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(0,'utf8')),null,2))" < data.json
Next steps: minify the result for production, or compare two JSON files. The JSON minification guide explains when each layout is right. Your JSON is processed in your browser and is not uploaded.