Skip to content

How to format and pretty print JSON

Turn a one-line JSON blob into readable, indented JSON online, in code or on the command line, and minify it again when you're done.

Guide4 min readUpdated

APIs and log files usually send JSON on a single line to save space. Formatting (also called pretty printing or beautifying) adds line breaks and indentation so you can actually read it. It only changes whitespace: the data stays exactly the same.

{"id":7,"name":"Ann","roles":["admin","dev"],"active":true}
Minified
{
  "id": 7,
  "name": "Ann",
  "roles": ["admin", "dev"],
  "active": true
}
Formatted

Format JSON online

The fastest way is to paste it into the JSON Formatter. It formats as you type, with 2-space, 4-space or tab indentation and optional key sorting, highlights the syntax, and tells you the exact line if the JSON is invalid. It runs entirely in your browser, so API responses, tokens and customer data never leave your device. Large or deeply nested JSON is easier to read in the Tree Viewer.

In JavaScript and Node.js

JSON.stringify takes the indentation as its third argument: a number of spaces or a string such as "\t".

const data = JSON.parse(text);

JSON.stringify(data, null, 2);    // 2 spaces
JSON.stringify(data, null, "\t"); // tabs
JSON.stringify(data);             // minified, no whitespace

To pretty print a file from the terminal with Node:

node -e 'console.log(JSON.stringify(JSON.parse(require("fs").readFileSync(0, "utf8")), null, 2))' < data.json

In Python

import json

data = json.loads(text)
print(json.dumps(data, indent=2))                     # 2 spaces
print(json.dumps(data, indent=2, sort_keys=True))     # keys sorted
print(json.dumps(data, indent=2, ensure_ascii=False)) # keep é, ü, 中 as is

Python also ships a command-line formatter, so no extra install is needed:

python3 -m json.tool data.json                       # 4 spaces
python3 -m json.tool --indent 2 --sort-keys data.json
python3 -m json.tool --compact data.json             # minify

By default json.dumps and json.tool escape non-ASCII characters, so Zoë becomes Zoë. Use ensure_ascii=False or --no-ensure-ascii to keep them readable.

On the command line with jq

jq is the standard tool for JSON in a terminal. The filter . means “the whole input”, so it simply pretty prints it:

jq . data.json                  # format
jq -S . data.json               # format with sorted keys
jq -c . data.json               # minify to one line
curl -s https://api.example.com/users | jq .

In VS Code and other editors

In VS Code, open the file and run Format Document: Shift+Alt+F on Windows, Shift+Option+F on macOS and Ctrl+Shift+I on Linux. If the JSON isn't in a .json file, set the language to JSON first from the status bar. JetBrains IDEs use Reformat Code (Ctrl+Alt+L, or ⌥+⌘+L on macOS).

Minifying JSON again

Before sending JSON over the network or storing it, you can remove the whitespace again to make it smaller. The JSON Minifier does this without touching your numbers or escapes and shows how much space you saved; in code, JSON.stringify(data) with no indentation does the same.

If formatting fails

A formatter can only format valid JSON. If you get an error instead, the JSON has a syntax problem such as a trailing comma or single quotes. The JSON Validator shows exactly where, and our guide to fixing JSON errors explains each message.

Frequently asked questions

Does formatting change my JSON data?

No. Formatting only adds or removes whitespace between values, so the data is identical. Sorting keys changes the order of keys in objects, which doesn't change their meaning.

2 spaces or 4 spaces?

Either is fine; it's a style choice. 2 spaces is the most common in JavaScript projects and APIs, while Python's json.tool defaults to 4.

Why does Python show \u00eb instead of ë?

json.dumps escapes non-ASCII characters by default. Pass ensure_ascii=False (or --no-ensure-ascii to json.tool) to keep them as they are.

Tools used in this guide

More guides