{ }jsonkitOpen

How to format and beautify JSON

6 min read · updated 2026-07-12

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:

StyleProsCommon in
2 spacesCompact. Less horizontal drift on deep nesting. The usual web defaultnpm/package.json, most JS/TS projects, Prettier default
4 spacesMore visual separation between levels. Easier to scanPython ecosystems, some enterprise style guides
TabsReader controls display width. Smaller file sizeGo-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

Minified inputjson
{"id":42,"name":"Ada","roles":["admin","editor"],"active":true}
Same data, formatted with 2-space indentationjson
{
  "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.

JSON FormatterOpen the tool→

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.

  1. Open the file, or paste your JSON into an empty buffer and set the language mode to JSON.
  2. Run Format Document: Shift+Alt+F on Windows/Linux, Shift+Option+F on macOS.
  3. 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:

VS Code settings.json with 2-space JSON indentationjson
{
  "[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.

Pretty-print with JSON.stringifyjs
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

jq pretty-prints by default; use --indent or --tab to control widthbash
# 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.json

The . 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

Python's built-in JSON formatterbash
# 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.json

json.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.

Frequently asked questions

What is the standard indentation for JSON?

There is no official standard, since JSON ignores whitespace. The usual convention on the web is 2 spaces. npm's package.json uses it and Prettier emits it by default. Four spaces and tabs are also valid. Being consistent within a project matters more than which one you pick.

Does formatting JSON change the data?

No. Formatting only adds or removes whitespace between tokens. Keys, key order, numbers, strings, booleans and nesting stay the same. You can beautify and minify a document as often as you like without changing its meaning.

How do I pretty-print JSON in JavaScript?

Use the third argument of JSON.stringify. JSON.stringify(value, null, 2) indents by 2 spaces. Pass 4 for four spaces or "\t" for tabs. To reformat an existing string, parse it first with JSON.stringify(JSON.parse(raw), null, 2).

How do I format a JSON file from the command line?

Use jq . file.json, which indents by 2 spaces by default. Change that with --indent N or --tab. Or use python3 -m json.tool file.json, which comes with Python and indents by 4 spaces unless you pass --indent N. Both validate the input while they parse it.

How do I format JSON in VS Code?

Set the file to JSON language mode and run Format Document. The shortcut is Shift+Alt+F on Windows/Linux or Shift+Option+F on macOS. Change the indent width with the 'Spaces: N' button in the status bar. For a permanent default, set editor.tabSize under a "[json]" block in settings.json.

Why does my JSON formatter say the input is invalid?

A formatter must parse JSON before it can pretty-print it. Any syntax error stops it, such as a trailing comma, a single quote, an unquoted key or a missing bracket. Fix the syntax first (a validator shows the line and column), then format. Comments and trailing commas are JSON5/JSONC features that strict JSON parsers reject.