Skip to content
How-To

JSON Pretty Print: Online, CLI, Python & JavaScript

·8 min read
JSON Pretty Print: Online, CLI, Python & JavaScript - How-To guide from JSON Prettifier

Quick answer: Pretty printing adds indentation and line breaks to compressed JSON without changing the data. In JavaScript use JSON.stringify(data, null, 2), in Python use json.dumps(data, indent=2), and on the command line run jq . file.json or python -m json.tool file.json. For a one-off, paste the JSON into a browser formatter and copy the result.

You hit an API. The response comes back as a single, unbroken wall of text - brackets, colons, commas, all jammed together on one line. Good luck finding the field you need in that mess.

That is where JSON pretty print comes in. It takes that compressed blob and turns it into something you can actually read - with proper indentation, line breaks, and structure.

In this guide, you will learn how to pretty print JSON online, from the command line, and in every major programming language. Each method includes copy-paste examples you can use right now.

Open a valid sample in the JSON Viewer to explore objects and arrays as collapsible nodes alongside the formatted code.

What Does "Pretty Print" Actually Mean?

Pretty printing is the process of adding whitespace - indentation and line breaks - to compressed JSON so humans can read it. The data stays exactly the same. Only the formatting changes.

Here is what minified JSON looks like:

{"users":[{"name":"Dana","age":30,"roles":["admin","editor"]},{"name":"Alex","age":25,"roles":["viewer"]}]}

And here is the same data after pretty printing:

{
  "users": [
    {
      "name": "Dana",
      "age": 30,
      "roles": ["admin", "editor"]
    },
    {
      "name": "Alex",
      "age": 25,
      "roles": ["viewer"]
    }
  ]
}

Same data. Completely different experience. You can instantly see the structure, spot missing fields, and trace nested objects. If you want the reverse operation - stripping whitespace for production - check out our guide on how to minify JSON.

Pretty Print JSON Online

The fastest way to pretty print JSON is to use an online tool. No installation. No terminal. You paste your JSON, and it formats instantly.

Our JSON pretty print online tool validates your input as you type, highlights syntax errors in real time, and lets you switch between 2-space and 4-space indentation. You can copy the result or download it as a file.

This is the right choice when you are debugging on someone else's machine, inspecting a webhook payload during an incident, or working on a device where you cannot install anything. If you have a browser, you are good to go. And if the response is already open in a Chrome tab, you can pretty print it inside DevTools without copying it anywhere.

Pretty Print JSON in JavaScript

JavaScript gives you pretty printing out of the box with JSON.stringify(). The trick is the third argument - it controls indentation:

const data = {
  name: "Dana",
  age: 30,
  roles: ["admin", "editor"]
};

// Pretty print with 2-space indent
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);

Output:

{
  "name": "Dana",
  "age": 30,
  "roles": [
    "admin",
    "editor"
  ]
}

The second argument is a replacer function or array - passing null means "include everything." The third argument is your indent size. Pass 2 for two spaces, 4 for four, or even "\t" for tabs.

For a deeper dive into working with JSON in the browser and Node.js, see our guide on working with JSON in JavaScript.

Pretty Print JSON in Python

Python's built-in json module handles pretty printing with zero extra dependencies:

import json

data = {
    "name": "Dana",
    "age": 30,
    "roles": ["admin", "editor"]
}

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

The indent parameter accepts any positive integer. Use indent=2 for compact output or indent=4 to match PEP 8 conventions.

You can also sort keys alphabetically - useful when you want consistent output across runs:

json.dumps(data, indent=2, sort_keys=True)

The json module has more options worth knowing - ensure_ascii, default, and separators among them - and our Python json module guide walks through each one with examples.

Pretty Print JSON from the Command Line

Two tools cover most command-line scenarios: jq and Python's built-in json.tool.

Using jq

jq is the gold standard for command-line JSON processing. Pretty printing is its default behavior:

echo '{"name":"Dana","age":30}' | jq .

You can also format a file directly:

jq . data.json

Need a specific indent size? Use the --indent flag:

jq --indent 4 . data.json

Formatting is only the start of what jq does. Once you need to pull a field out of the output or filter an array, keep our jq cheat sheet open - it covers the filters you will reach for most often.

Using python -m json.tool

If Python is installed, you already have a pretty printer - no packages needed:

echo '{"name":"Dana","age":30}' | python -m json.tool

This outputs 4-space indented JSON by default. You cannot change the indent size without writing a script, but for a quick one-off format, it is unbeatable.

Pretty Print JSON in Node.js

In Node.js, you often need to read a JSON file, format it, and write it back. Here is how:

const fs = require("fs");

// Read and parse
const raw = fs.readFileSync("data.json", "utf-8");
const data = JSON.parse(raw);

// Write pretty-printed output
fs.writeFileSync(
  "data-formatted.json",
  JSON.stringify(data, null, 2) + "\n"
);

Adding \n at the end ensures the file ends with a newline, which keeps Git diffs clean and follows POSIX conventions.

For larger files, you might want to stream the data instead of loading everything into memory. But for config files, API fixtures, and anything under a few megabytes, the approach above works perfectly. Our guide to reading and writing JSON files in Node.js covers the async and streaming variants when you need them.

Custom Indentation - Tabs vs. Spaces

The two most common choices are 2 spaces and 4 spaces. The JavaScript ecosystem has largely settled on 2 spaces - package.json, tsconfig.json, and most linter configs all use it. Python projects tend toward 4 spaces, matching PEP 8.

If you prefer tabs, both JSON.stringify() and jq support them:

// JavaScript - tabs
JSON.stringify(data, null, "\t");

# jq - tabs
jq --tab . data.json

For deeply nested JSON, 2 spaces keep lines shorter and easier to scan. For flat structures, it barely matters. The real rule is simple - pick what your team uses and stay consistent. For more on formatting choices, check out our full guide on how to format JSON.

When NOT to Pretty Print

Pretty printing is for humans. It is not for machines. There are cases where you should keep JSON minified:

  • Production API responses - whitespace adds bytes to every response. A typical JSON payload shrinks 15-30% when minified. At scale, that is real bandwidth and real money.
  • Storage optimization - if you are storing millions of JSON documents in a database or object store, minified JSON saves meaningful disk space.
  • Network-constrained environments - mobile apps on slow connections, IoT devices, and edge functions all benefit from smaller payloads.
  • Log ingestion - structured logging systems expect one JSON object per line, minified. Pretty printing breaks line-based parsing. This one-object-per-line format is called JSON Lines (NDJSON), and it is also the format to reach for when a file is too big to parse in one go.

The pattern is straightforward. Pretty print for debugging, development, and config files you check into version control. Minify for everything that goes over a wire or into storage.

Frequently Asked Questions

What does it mean to pretty print JSON?
It means adding whitespace, indentation, and line breaks to compressed JSON so people can read it. The values and structure stay identical; only the formatting changes.
How do I pretty print JSON in JavaScript?
Call JSON.stringify(data, null, 2). The third argument sets the indent: pass 2 or 4 for spaces, or the string "\t" for tabs. The second argument is a replacer; passing null includes every property.
How do I pretty print JSON in Python?
Use json.dumps(data, indent=2) from the built-in json module. Add sort_keys=True for alphabetically ordered keys, or run python -m json.tool on a file or piped input to get 4-space output without writing any code.
How do I pretty print a JSON file from the command line?
Run jq . data.json. Pretty printing is jq's default behavior, and it accepts --indent 4 or --tab for other styles. If jq is not installed, python -m json.tool data.json does the same job with 4-space indentation.
Should JSON be pretty printed in production?
No. Keep API responses, stored documents, and log lines minified, since whitespace adds bytes to every payload and line-based log systems expect one object per line. Pretty print for debugging, development, and config files kept in version control.

Pretty Print JSON Instantly

Paste your JSON and get perfectly formatted output in one click. No signup, no installation, 100% private in your browser.

Open JSON Prettifier