</>CodeScrub

What Is JSON Pretty Print (and How to Do It Instantly)

JSON pretty print adds indentation and line breaks to make JSON readable. Here's how to do it online, in code, and from the command line.

5 min read
  • json
  • formatting
  • pretty-print
  • beginner

title: "What Is JSON Pretty Print (and How to Do It Instantly)" description: "JSON pretty print adds indentation and line breaks to make JSON readable. Here's how to do it online, in code, and from the command line." date: "2026-06-08" tags: ["json", "formatting", "pretty-print", "beginner"] relatedTool: "json-formatter" published: true

JSON pretty print means adding indentation, line breaks, and spacing to make a JSON document readable by a human. APIs and databases usually return minified JSON — everything crammed onto a single line without whitespace — because it's smaller and faster to transmit. Pretty printing reverses that: it doesn't change the data, only the formatting. Every value, key, and structure stays identical; the pretty version just breathes.

Pretty Print JSON Online (Fastest)

Paste your JSON into the CodeScrub JSON Formatter and get formatted output in the browser instantly. Nothing is sent to a server, so you can safely paste API responses that contain internal identifiers or private data.

Before (minified, one long line):

{"users":[{"id":1,"name":"Ada Lovelace","email":"ada@example.com","active":true},{"id":2,"name":"Alan Turing","email":"alan@example.com","active":false}]}

After (pretty printed with 2-space indentation):

{
  "users": [
    {
      "id": 1,
      "name": "Ada Lovelace",
      "email": "ada@example.com",
      "active": true
    },
    {
      "id": 2,
      "name": "Alan Turing",
      "email": "alan@example.com",
      "active": false
    }
  ]
}

Same bytes on the wire before parsing; same data after. Only the whitespace between tokens changes.

Pretty Print in Code

If you're doing this from a script or an app, every major language has pretty-print built in — no extra library required.

JavaScript / TypeScript:

const data = { name: "Ada", role: "admin" };
const pretty = JSON.stringify(data, null, 2);
console.log(pretty);

JSON.stringify takes three arguments: the value to serialize, a replacer (usually null), and an indent size in spaces. Pass 2 for the common two-space format, 4 for wider indentation, or the string "\t" for tabs.

Python:

import json

data = {"name": "Ada", "role": "admin"}
pretty = json.dumps(data, indent=2)
print(pretty)

Add sort_keys=True if you want alphabetically ordered keys — handy for diff-friendly output when the same data is written multiple times.

PHP:

$data = ["name" => "Ada", "role" => "admin"];
echo json_encode($data, JSON_PRETTY_PRINT);

Combine flags with the | operator if you also want raw Unicode: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE.

Pretty Print from the Command Line

For quick one-offs in a terminal — inspecting an API response, cleaning up a log line — pipe the JSON through one of these:

jq (the standard tool for JSON on the command line):

echo '{"name":"Ada","role":"admin"}' | jq .

The . at the end is a jq filter meaning "the entire value" — you can also use jq '.users[0]' to drill in, or jq .name to pluck a single field.

Python (already installed on macOS and most Linux distros, no extra tools needed):

echo '{"name":"Ada"}' | python3 -m json.tool

Node.js:

echo '{"name":"Ada"}' | node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>console.log(JSON.stringify(JSON.parse(d),null,2)))"

jq is the best option if you're going to be doing this more than once. Install it with brew install jq on macOS, apt install jq on Ubuntu/Debian, or choco install jq on Windows with Chocolatey.

Pretty Print vs Minify

Pretty print and minify are opposites — the same operation running in different directions.

Pretty print adds whitespace: indentation, line breaks, and a space after each colon. It exists to make JSON readable during debugging, code review, or when someone needs to eyeball the structure.

Minify removes every character that isn't strictly needed to parse the JSON: no indentation, no newlines, no spaces between tokens. The result is smaller — often 20–40% smaller than the pretty version — which matters for network payloads and storage.

Use pretty print while you're working with the data. Use minify when the data is going to production, into a database column, or across the wire to an end user. The CodeScrub JSON Formatter toggles between the two with a single button, so you don't need to think about it.

Indent Size: 2 Spaces vs 4 Spaces vs Tabs

Two spaces is the dominant convention for JSON. It's what npm's package.json, VS Code's default formatter, and most public APIs produce. Four spaces is more visually distinct at a glance but makes deeply nested JSON very wide — a five-levels-deep object can push past 80 columns just from indentation. Tabs are rare in JSON but a handful of teams prefer them (a tab char takes one byte on the wire regardless of visual width, and each dev can render at their preferred width). Pick one, put it in your .editorconfig, and be consistent — the specific choice matters far less than the consistency does.

Pretty Print in VS Code

Open the JSON file in VS Code and press Shift + Alt + F on Windows or Linux, or Shift + Option + F on macOS, to auto-format the entire document. VS Code uses its built-in JSON formatter, which respects the editor's tab size setting — change "editor.tabSize" to 2 or 4 in your user or workspace settings to control the indent width. Turn on "editor.formatOnSave": true and every save auto-formats, so you never have to think about pressing the shortcut again.

Bottom line

JSON pretty print is a two-second task — pasting into a tool, running one CLI command, or pressing one editor shortcut. Don't waste time reformatting by hand: paste, format, copy, done.

Ad space · blog-article-mid