How to Pretty Print JSON – Browser, JavaScript, Python, curl

Pretty printing JSON adds indentation and line breaks to compact JSON to make it human-readable — the data is identical, only whitespace changes. In JavaScript: JSON.stringify(data, null, 2). In Python: json.dumps(data, indent=2). In the terminal: cat file.json | python -m json.tool. Or paste directly into the JSON Formatter for instant formatting in your browser.

In the browser — the fastest option for one-off inspection

If you have a JSON blob from an API response or a log file and need to read it right now, paste it into the JSON Formatter. It formats the JSON instantly with a collapsible tree view and 2-space indentation. No setup, no install — works entirely in your browser.

In JavaScript — JSON.stringify with the space parameter

JSON.stringify(value, replacer, space) — the third argument controls indentation:

const data = {
    name: "Alice",
    age: 28,
    roles: ["admin", "editor"],
    address: { city: "London", country: "UK" }
};

// 2-space indent (most common)
console.log(JSON.stringify(data, null, 2));

// 4-space indent
console.log(JSON.stringify(data, null, 4));

// Tab indent
console.log(JSON.stringify(data, null, '\t'));

Pass null as the second argument to include all keys. See the full guide on JSON.stringify indent options for more on the replacer parameter.

In Python — json.dumps with indent

import json

data = {"name": "Alice", "age": 28, "roles": ["admin", "editor"]}

# 2-space indent
print(json.dumps(data, indent=2))

# Sorted keys + 2-space indent
print(json.dumps(data, indent=2, sort_keys=True))

For more Python-specific options including file reformatting, see How to Format JSON in Python.

In curl — pipe to python -m json.tool or jq

The quickest way to pretty-print an API response directly in your terminal:

# Using Python's built-in json.tool (no extra install required)
curl -s https://api.example.com/users | python -m json.tool

# Using jq (if installed — more powerful, colorised output)
curl -s https://api.example.com/users | jq '.'

# Format and save to file
curl -s https://api.example.com/users | python -m json.tool > users-formatted.json

In Node.js

const https = require('https');

https.get('https://api.example.com/data', (res) => {
    let body = '';
    res.on('data', chunk => body += chunk);
    res.on('end', () => {
        const data = JSON.parse(body);
        // Pretty-print to console
        console.log(JSON.stringify(data, null, 2));
    });
});

In VS Code

Open a .json file and press Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac) to format the document using VS Code's built-in JSON formatter. This respects your editor's indentation settings.

When to pretty-print vs when to minify

Pretty print during development, debugging, and code review — it makes structures clear at a glance. Minify for production payloads where bandwidth matters. Use the JSON Minifier to strip all whitespace before shipping JSON over the network.

Pretty-printing large or streamed JSON

For very large files, loading the whole document into memory just to reformat it can be slow or impossible. Command-line jq is the best tool here because it streams: jq '.' big.json formats a multi-megabyte file without holding a formatted copy of the entire thing in an editor. If the data is newline-delimited JSON (NDJSON / JSON Lines), don't try to format it as one document — each line is its own JSON value. Format them individually with jq -c '.' data.ndjson to keep one object per line, or jq '.' data.ndjson to expand each. For an occasional large blob you just need to read once, the browser-based JSON Viewer renders it as a collapsible tree so you can drill into the branch you care about instead of scrolling thousands of formatted lines.

Common pretty-printing pitfalls

A few surprises catch people out. First, non-ASCII characters: Python's json.dumps escapes them to \uXXXX by default, so an accented name or emoji looks mangled — pass ensure_ascii=False to keep the real characters. Second, key order: most formatters preserve insertion order, but adding sort_keys=True (Python) or piping through jq -S gives you alphabetical keys, which makes two JSON files diff cleanly. Third, trailing output: console.log and print add a newline, which is usually fine, but if you're writing to a file that another tool parses strictly, make sure you're not adding a stray blank line. Finally, remember that pretty-printing is purely cosmetic — a formatted and a minified version of the same data parse to identical objects, so never rely on whitespace to carry meaning.

Try it directly in your browser — free, no signup:

Open JSON Formatter →

Frequently Asked Questions

What does 'pretty print' JSON mean?

Pretty printing (also called formatting or beautifying) adds indentation and line breaks to compact JSON to make it human-readable. The data is identical — only the whitespace changes. A JSON formatter parses the compact JSON and re-serializes it with consistent indentation.

How do I pretty print JSON in JavaScript?

Use JSON.stringify(data, null, 2) where the third argument is the number of spaces to indent. For example: JSON.stringify({name:'Alice', age:28}, null, 2) produces a formatted 2-space-indented JSON string. Pass 4 for 4-space indentation, or '\t' for tab indentation.

How do I pretty print JSON from curl?

Pipe the curl output to Python's json.tool: curl -s https://api.example.com/data | python -m json.tool. If you have jq installed, use: curl -s https://api.example.com/data | jq '.'

Does pretty printing change the JSON data?

No. Pretty printing only changes whitespace — spaces, tabs, and line breaks. The keys, values, and structure are exactly the same. A minified and a formatted version of the same JSON parse to identical objects.

About the author

Pasindu Ishan is a Senior Software Engineer working on data streaming systems. These guides are about writing JSON that other people’s parsers will accept, including the parts of the format that are stricter than they look. He builds the free in-browser tools at JSON Dev Tools.