Skip to content
How-To

JSON Pretty Print: Online, CLI, Python & JavaScript

·8 min read

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.

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.

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 nullmeans "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)

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

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.

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:

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.

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