Pretty-print JSON by parsing it and re-serializing it with indentation: in Python json.dumps(obj, indent=4), in JavaScript JSON.stringify(obj, null, 2), and on the command line python -m json.tool file.json. All three work on any valid JSON, and the indent value controls column width without changing the data itself.

The examples below target Python 3.8+ and modern Node.js or browsers, and the browser route covers Edge's built-in viewer for JSON files and server responses.

Quick Syntax Reference

Paste-ready commands for the four most common environments:

# Python: validate and pretty-print a file to stdout
python -m json.tool data.json

# Python: pretty-print with tabs and sorted keys
python -m json.tool --indent $'\t' --sort-keys data.json
import json

with open("data.json", encoding="utf-8") as f:
    obj = json.load(f)

pretty = json.dumps(obj, indent=4, sort_keys=True, ensure_ascii=False)
print(pretty)
// Node.js or browser
const pretty = JSON.stringify(obj, null, 2);
console.log(pretty);
Tool Call Default indent Notes
Python json.dumps json.dumps(obj, indent=4) None (single line) Accepts integer or string like "\t"
Python CLI python -m json.tool file.json 4 spaces --indent, --sort-keys, --compact
JavaScript JSON.stringify(obj, null, 2) none (single line when omitted) Third argument is spaces or fill string
Edge browser Pretty-print checkbox n/a View-only; does not modify the file

What Indentation Options Actually Do

The indent value is the only thing that changes between pretty and compact output. In Python, a positive integer indents that many spaces per level; a string such as "\t" is used verbatim as the indent unit; 0, a negative number, or "" inserts newlines only; None (the default) produces no newlines at all. That last distinction matters—indent=0 is not the same as omitting indent.

In JavaScript, JSON.stringify(value, replacer, space) treats the third argument as the number of spaces (capped at 10) or a string of up to 10 characters used as the indent. Passing undefined or omitting it yields minified output. If you pass a replacer function as the second argument, keep it null when you only want spacing.

Pretty-printing is a presentation step: it parses the input into a data structure and serializes it back. Whitespace, key order (unless you sort), and number formatting follow the serializer's rules, not the original file's bytes. That's why you shouldn't pretty-print and save a file you don't own—comments and trailing commas that many hand-edited JSON files carry will be rejected or dropped.

Worked Example 1: Pretty-Print a File from Python

Suppose data.json contains the minified line:

{"id":42,"name":"widget","tags":["a","b"],"active":true}

Running python -m json.tool data.json prints:

{
    "id": 42,
    "name": "widget",
    "tags": [
        "a",
        "b"
    ],
    "active": true
}

To write the result back to a new file instead of stdout, use shell redirection: python -m json.tool data.json > pretty.json. Confirm it worked by re-running the command against pretty.json—identical output means the round trip is stable. If the command exits with Expecting property name enclosed in double quotes or Extra data, the source isn't valid JSON; fix the syntax (often a trailing comma or single quotes) before retrying.

Worked Example 2: Indent Control and Key Sorting in Python

Start with this dictionary in a Python script:

obj = {"6": 7, "4": 5, "nested": {"x": 1}}

json.dumps(obj, sort_keys=True, indent=4) produces:

{
    "4": 5,
    "6": 7,
    "nested": {
        "x": 1
    }
}

Two practical variations. First, use a tab for indent: json.dumps(obj, indent="\t") emits literal tab characters, which is handy when your editor is configured for tabs. Second, when outputting JSON that contains non-ASCII text, add ensure_ascii=False so accented characters and non-Latin scripts print as themselves instead of \uXXXX escapes. Verify by reading the first few lines with a text editor—if you see unbroken \u sequences, ensure_ascii was left at its default True.

Worked Example 3: Pretty-Print in JavaScript and the Browser

Given this object in a Node.js script or a browser console:

const obj = {"id":42,"name":"widget","tags":["a","b"],"active":true};

JSON.stringify(obj, null, 2) returns the same structure with two-space indentation. If you're logging an API response that arrives as a string, parse first, then stringify: JSON.stringify(JSON.parse(raw), null, 2). Skipping the parse step double-encodes the result and escapes every quote, which is the usual cause of "my output has backslashes everywhere."

For JSON you're viewing rather than generating, Microsoft Edge includes a built-in JSON viewer: open a .json URL or a JSON server response, then select the Pretty-print checkbox to switch between minified and formatted display. Toggling the checkbox changes only how the response is displayed—the underlying file is untouched. If the document renders as plain text instead of the structured viewer, the response's Content-Type is likely not application/json; check that header server-side rather than re-encoding the payload.

Troubleshooting Pretty-Print Errors

Symptom Cause Verified fix
Expecting property name enclosed in double quotes Trailing comma or single quotes in the source Remove the trailing comma; replace single with double quotes
Extra data Multiple JSON documents or text after the object Wrap values in an array or isolate one document
json.decoder.JSONDecodeError: Expecting value Empty file or invalid starting token Confirm the file begins with {, [, or a valid literal
Output shows \u00e9-style escapes ensure_ascii default is True Set ensure_ascii=False in json.dumps
Output is one long line indent omitted or None Pass indent=2 (or another positive value)
JavaScript output full of \" Stringify called on a string, not an object JSON.parse first, then JSON.stringify
Edge JSON viewer not shown Response Content-Type not application/json Verify the header at the server or with the Network tool

The Python json module's own documentation covers the indent parameter's edge cases in full, including the string and zero-value behavior, at the CPython json reference. The Edge viewer's behavior is documented in Microsoft Edge's JSON viewer page.

Frequently Asked Questions

Does pretty-printing change the JSON data?

No. It parses the input and re-serializes it with whitespace, so values stay the same; only formatting, key order (if you sort), and escape representation can differ. Never pretty-print and overwrite a file you didn't author, since comments and trailing commas aren't valid JSON.

What indent value is standard?

Two or four spaces are the common choices; neither is mandated by the JSON specification. Use 2 for web payloads and 4 for readability in source code, and stick to one per project.

Can I pretty-print without running code?

Yes. In a browser, open the JSON file or response and use the viewer's Pretty-print checkbox; the display changes while the file stays minified.