How-To

How to Format JSON: 5 Methods That Actually Work in 2026

9 min read

You copy an API response. It is one line. Four hundred characters of nested brackets, quotes, and commas. You need to find one field buried inside it. Good luck.

Formatting JSON is not about making things pretty. It is about making them readable. Here are five methods that actually work, from the fastest option to the most programmable.

Method 1: Online JSON Formatter (Fastest)

The fastest way to format JSON is to paste it into an online tool. No installation, no terminal, no configuration. You paste, you read, you move on.

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

Online formatters are ideal when you are debugging an API response on a colleague's laptop, inspecting a webhook payload on your phone during an outage, or working on a restricted machine where you cannot install tools. If there is a browser, it works.

Method 2: Command Line with jq

jq is the Swiss Army knife of command-line JSON processing. Install it once and you will use it every day:

# macOS
brew install jq

# Ubuntu/Debian
sudo apt install jq

# Windows (with scoop)
scoop install jq

Basic formatting is one command:

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

But jq does far more than pretty-printing. You can extract fields, filter arrays, and build pipelines:

# Extract a single field
echo '{"name":"Dana","age":30}' | jq '.name'

# Format a file with 2-space indent
jq --indent 2 . data.json

# Get the first item from an array
curl -s https://api.example.com/users | jq '.[0]'

jq pays for itself within a week. If you work with JSON on the command line regularly, it is the single most valuable tool you can install.

Method 3: Python's json.tool

If you have Python installed (and you probably do), you already have a JSON formatter. No extra packages needed:

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

Output:

{
    "name": "Dana",
    "age": 30
}

It uses 4-space indentation by default. You can also pass a file path directly:

python -m json.tool data.json

The trade-off is limited customization - you cannot change the indent size without writing a script. But for a quick one-off format, it is hard to beat something that ships with the language.

Method 4: VS Code

Visual Studio Code formats JSON files natively. Open any .json file and use the keyboard shortcut:

Even better, you can enable format-on-save so your JSON files are always clean:

// settings.json
{
  "[json]": {
    "editor.tabSize": 2,
    "editor.formatOnSave": true
  }
}

With this configuration, every time you save a JSON file, VS Code automatically formats it with 2-space indentation. This is especially useful for config files checked into version control - no more messy diffs caused by inconsistent formatting.

Method 5: JavaScript's JSON.stringify()

JSON.stringify() accepts three arguments. The third one controls indentation:

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

// Minified (no formatting)
JSON.stringify(data);
// '{"name":"Dana","age":30,"roles":["admin","editor"]}'

// Pretty-printed with 2 spaces
JSON.stringify(data, null, 2);

// Pretty-printed with 4 spaces
JSON.stringify(data, null, 4);

The second argument is a replacer function or array - passing nullmeans "include everything." The third argument is the indent: a number for spaces or a string for custom indentation.

A practical use case - writing a formatted JSON file in Node.js:

const fs = require("fs");

const config = {
  port: 3000,
  database: { host: "localhost", name: "myapp" }
};

fs.writeFileSync(
  "config.json",
  JSON.stringify(config, null, 2) + "\n"
);

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

Choosing Your Indent Size

The two 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 use 2-space indentation. Python tends toward 4 spaces, matching PEP 8 conventions.

For deeply nested JSON, 2 spaces keep lines shorter and more scannable. For flat structures, it barely matters. Pick whichever your team uses and stay consistent. The worst indent style is an inconsistent one.

When to Minify Instead

Formatting is about readability. But sometimes you need the opposite - removing all whitespace to reduce file size for production payloads. A typical JSON response shrinks by 15–30% when minified, and for APIs serving thousands of clients, that adds up fast.

For a complete guide on stripping whitespace from JSON, see how to minify JSON. And if you are brand new to the format itself, start with our introduction to what JSON is and how it works.

Format JSON Instantly

Need to format JSON right now? Paste it into our online formatter - instant results, zero setup, 100% private.

Open JSON Prettifier