How to Pretty-Print and Minify JSON
The same JSON can be written on hundreds of indented lines for people to read, or squeezed onto a single line for computers to send. Formatting (also called pretty-printing or beautifying) and minifying (condensing) only change the whitespace, never the data. Here's how to do both wherever you happen to be working.
Pretty vs minified: what changes
These two documents contain exactly the same data:
{
"id": 5001,
"customer": "Maria Garcia",
"items": [
"Coffee mug",
"Green tea"
],
"paid": true
}{"id":5001,"customer":"Maria Garcia","items":["Coffee mug","Green tea"],"paid":true}JSON ignores whitespace between values: spaces, tabs and line breaks. Formatting adds whitespace to show the structure, and minifying removes all of it. Whitespace inside strings, like the space in "Maria Garcia", is part of the data and is always kept.
Both operations need valid JSON, because the tool has to understand the structure to rearrange it. If formatting fails, fix the error first: common JSON errors covers the usual suspects.
In the browser
Paste your JSON into the JSON tool and press Prettify to spread it over indented lines, or Condense to put it back on one line. Copy Condensed copies a minified version without changing what's in the editor. Everything runs on your device, so it's safe for API responses that contain private data.
Most browsers also format JSON automatically when you open a JSON URL directly. Firefox has a built-in JSON viewer with a "Pretty Print" option, and recent versions of Chrome show a "pretty-print" checkbox.
On the command line with jq
jq is a small, free command-line JSON processor. It's available through Homebrew (brew install jq), most Linux package managers, and as a download for Windows. The simplest jq filter, ., means "the whole document", and jq pretty-prints its output by default:
jq . data.json # pretty-print with 2-space indent
jq --indent 4 . data.json # 4-space indent
jq --tab . data.json # indent with tabs
jq -c . data.json # minify (compact output)
jq -S . data.json # sort keys alphabetically
curl -s https://api.example.com/orders | jq . # format an API response
jq -c . data.json > data.min.json # save a minified copyjq also validates: if the input isn't valid JSON, it prints an error with a line and column and exits with a non-zero status, which makes it useful in scripts and CI checks.
On the command line with Python
If Python is installed, you already have a JSON formatter. The json.tool module reads a file (or standard input) and prints it formatted, with a 4-space indent by default:
python3 -m json.tool data.json # pretty-print, 4-space indent
python3 -m json.tool --indent 2 data.json # 2-space indent
python3 -m json.tool --sort-keys data.json # sort keys
python3 -m json.tool --compact data.json # minify (Python 3.9+)
python3 -m json.tool data.json pretty.json # write the result to a fileOn Windows, use python or py instead of python3.
In VS Code
Open a .json file and run Format Document: press Shift+Alt+F on Windows, Shift+Option+F on macOS, or Ctrl+Shift+I on Linux. You can also find it in the Command Palette. If you pasted JSON into a new, unsaved file, set the language to JSON first using the language picker in the bottom-right corner. VS Code doesn't include a minify command, but extensions add one, or you can use one of the other methods here.
In JavaScript and Python code
In code, formatting and minifying are just options on the function that writes JSON:
const data = JSON.parse(text);
const pretty = JSON.stringify(data, null, 2); // 2-space indent
const tabbed = JSON.stringify(data, null, "\t"); // tabs
const minified = JSON.stringify(data); // no whitespaceimport json
data = json.loads(text)
pretty = json.dumps(data, indent=2)
minified = json.dumps(data, separators=(",", ":")) # no spaces after , and :Note that Python's json.dumps(data) without arguments isn't fully minified: it puts a space after every comma and colon. Pass separators=(",", ":") to remove them.
Formatting can change more than whitespace
Tools that parse JSON into JavaScript and write it back out, including this site's Prettify button and JSON.stringify, normalize numbers along the way: 1.0 becomes 1, 1e3 becomes 1000, and integers above 253 lose precision. jq 1.7 and later keep the original precision, though it may still change how a number is written (1e3 comes out as 1E+3). If large IDs must survive exactly, store them as strings.
When to use each
- Format anything a person will read: config files, files committed to Git (so diffs show one changed line instead of one huge one), API responses you're debugging, and documentation examples.
- Minify anything only machines read, where size matters: API responses in production, data embedded in web pages, and JSON stored in databases or logs.
The size difference is real but often smaller than you'd think. The example above is 114 characters formatted and 84 minified, but servers usually compress responses with gzip or Brotli, which shrinks repeated whitespace very efficiently. Minifying still helps, just less dramatically over the network than on disk.