To format JSON (also called pretty-printing or beautifying), you add consistent indentation and line breaks to a minified blob so people can read and diff it. Every environment has a built-in way to do it.
This guide shows how to format JSON in a browser tool, in VS Code, with JSON.stringify, and on the command line with jq and Python's json.tool. Formatting only changes whitespace. Your data stays the same.
What "formatting" JSON means
Whitespace in JSON has no meaning. The parser ignores spaces, tabs and newlines between tokens, so {"a":1} and the same object spread over five indented lines are the same value. Formatting (or *beautifying*) adds that whitespace in a consistent pattern so people can see the structure.
A formatter puts each key/value pair on its own line. It indents nested objects and arrays by a fixed amount per level, and it adds one space after each colon. Nothing else changes. Keys keep their order, numbers keep their precision and strings stay as they are.
Choose an indentation style: 2 spaces, 4 spaces or tabs
The spec accepts all three. Pick the one that reads best and matches your team's conventions. Here is how they compare:
| Style | Pros | Common in |
|---|---|---|
| 2 spaces | Compact. Less horizontal drift on deep nesting. The usual web default | npm/package.json, most JS/TS projects, Prettier default |
| 4 spaces | More visual separation between levels. Easier to scan | Python ecosystems, some enterprise style guides |
| Tabs | Reader controls display width. Smaller file size | Go-adjacent tooling, accessibility-focused setups |
Two spaces is the safest default. package.json uses it, Prettier emits it and most linters expect it. Whatever you pick, use it across the whole repository. Mixed indentation annoys reviewers and makes diffs noisy.
A quick before-and-after
{"id":42,"name":"Ada","roles":["admin","editor"],"active":true}{
"id": 42,
"name": "Ada",
"roles": [
"admin",
"editor"
],
"active": true
}Format JSON in the browser (no install)
A browser tool is the fastest way to beautify a one-off payload, such as an API response from the network tab, a log line or a webhook body. Paste it in and it is formatted. If the tool runs client-side, nothing leaves your machine.
A good formatter also validates while it parses. Malformed input gives you an error with a line and column, and your data is not silently mangled. A formatter can only pretty-print input that parses, so fix syntax problems first. See how to fix common JSON syntax errors. To check validity without reformatting, use the JSON validator.
Format JSON in VS Code
VS Code formats JSON without an extension, as long as it recognizes the file as JSON. The status bar shows "JSON" as the language mode, or the file ends in .json.
- Open the file, or paste your JSON into an empty buffer and set the language mode to JSON.
- Run Format Document:
Shift+Alt+Fon Windows/Linux,Shift+Option+Fon macOS. - To change indentation, click the Spaces: N indicator in the status bar and pick Indent Using Spaces or Indent Using Tabs, then the width.
To set the default width for every JSON file, add this to your settings.json:
{
"[json]": {
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.defaultFormatter": "vscode.json-language-features"
}
}Format Document uses the tab size the file already has, so you can expand a compact file once without changing settings. If Prettier is installed, it replaces the built-in formatter and applies its own rules.
Format JSON with JSON.stringify in JavaScript
In JavaScript and Node, the built-in formatter is the third argument to JSON.stringify, the space parameter. Pass a number to indent by that many spaces, or a string like "\t" to indent with tabs.
const data = { id: 42, name: "Ada", roles: ["admin", "editor"] };
// 2-space indentation (most common)
console.log(JSON.stringify(data, null, 2));
// 4 spaces
console.log(JSON.stringify(data, null, 4));
// Tab indentation
console.log(JSON.stringify(data, null, "\t"));
// Round-trip: parse a string, then re-emit it formatted
const raw = '{"a":1,"b":[2,3]}';
console.log(JSON.stringify(JSON.parse(raw), null, 2));The middle argument (null above) is the *replacer*. Pass an array of key names to keep only those keys, or a function to transform values. For plain formatting, leave it null. Working with JSON in JavaScript covers stringify and parse in more depth.
Format JSON on the command line with jq and Python
For files and shell pipelines, two tools cover almost every case. jq is a JSON processor that pretty-prints by default. Python's json.tool comes with every Python install and needs no extra packages.
Using jq
# Beautify a file (jq defaults to 2-space indentation)
jq . data.json
# 4-space indentation
jq --indent 4 . data.json
# Tab indentation
jq --tab . data.json
# Format an API response inline
curl -s https://api.example.com/users | jq .
# Overwrite the file with a formatted version
jq . data.json > tmp.json && mv tmp.json data.jsonThe . is the identity filter. It passes the input through unchanged, and jq formats the output. To query and transform JSON, see the jq cheat sheet and the JSONPath cheat sheet.
Using Python's json.tool
# Beautify a file (default 4-space indentation) to stdout
python3 -m json.tool data.json
# Choose the indent width
python3 -m json.tool --indent 2 data.json
# Format from a pipe
echo '{"a":1,"b":[2,3]}' | python3 -m json.tool
# Sort keys for a stable, diff-friendly output
python3 -m json.tool --sort-keys data.jsonjson.tool validates while it parses. Malformed input prints an error and exits non-zero, so it works as a cheap JSON lint step in CI. For JSON in Python scripts, see the Python json module guide.
Beautify vs minify: round trips without data loss
Beautifying and minifying are exact inverses. Beautify adds whitespace for people. Minify removes it for machines and smaller payloads. Whitespace between tokens has no meaning, so you can convert back and forth as often as you like and the parsed value stays the same.
- Beautify when you read, debug, diff or commit config files. Readable diffs matter more than bytes.
- Minify when you send data over the network, store it in a cache or embed it in a bundle. Every byte counts there, and nobody reads it.
Formatting keeps your data but not always the exact byte layout. Depending on the engine, a formatter may rewrite numbers in canonical form, for example by normalizing an exponent or a trailing zero. It also drops comments, since standard JSON has none. If your file has comments or trailing commas, it is JSON5 or JSONC, not plain JSON, and a strict formatter will reject it.