PureTools

How to Format JSON: Complete Guide

PureTools Team· 7 min read
How to Format JSON: Complete Guide

How to Format JSON: Complete Guide

JSON (JavaScript Object Notation) is the lingua franca of web APIs. Every developer deals with it daily — debugging API responses, writing config files, or passing data between services. Yet most of us paste ugly one-liners into random online tools without thinking twice.

This guide covers everything: formatting for readability, validating structure, minifying for production, and the tools that make it painless.

Why JSON Formatting Matters

An API returns this:

{"users":[{"id":1,"name":"Alice","email":"alice@example.com","roles":["admin","editor"]},{"id":2,"name":"Bob","email":"bob@example.com","roles":["viewer"]}]}

Good luck reading that. Now formatted:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "email": "alice@example.com",
      "roles": ["admin", "editor"]
    },
    {
      "id": 2,
      "name": "Bob",
      "email": "bob@example.com",
      "roles": ["viewer"]
    }
  ]
}

The difference is immediate. You can spot missing fields, wrong types, and structural issues in seconds.

Formatting in the Terminal

If you have jq installed:

curl https://api.example.com/users | jq .

Python one-liner:

echo '{"a":1}' | python3 -m json.tool

Node.js:

echo '{"a":1}' | node -e "process.stdin.on('data',d=>console.log(JSON.stringify(JSON.parse(d),null,2)))"

Validating JSON

Common JSON errors that break parsing:

  • Trailing commas: {"a": 1,} — invalid in JSON (valid in JS)
  • Single quotes: {'a': 1} — JSON requires double quotes
  • Unquoted keys: {a: 1} — keys must be strings
  • Comments: // this breaks — JSON has no comments

When you get a "SyntaxError: Unexpected token", check these first. Most formatting tools validate automatically and show you exactly where the error is.

Minification for Production

Formatted JSON is great for humans. For production payloads, minification matters:

// 247 bytes formatted
{
  "users": [
    { "id": 1, "name": "Alice" }
  ]
}

// 39 bytes minified
{"users":[{"id":1,"name":"Alice"}]}

That's an 84% reduction. For API responses served millions of times, the bandwidth savings add up fast.

Indentation: 2 Spaces vs 4 Spaces vs Tabs

This is almost a religious debate, but here's the practical take:

  • 2 spaces: Most popular in JS/TS ecosystem. Google, Airbnb, and Prettier defaults use it.
  • 4 spaces: Common in Python, Java. Easier to read deeply nested structures.
  • Tabs: Accessibility-friendly (users set their own width), but rare in JSON.

JSON.stringify(data, null, 2) — the second argument is a replacer, the third is indentation.

Beyond Formatting: jq for Power Users

Once you're comfortable with JSON, jq opens up a world of data manipulation:

# Extract just names
curl api.example.com/users | jq '.users[].name'

# Filter by role
jq '.users[] | select(.roles[] == "admin")'

# Count items
jq '.users | length'

It's grep for JSON, and it's indispensable for debugging APIs from the terminal.

Try it now: Format your JSON instantly with our free online tool — paste, click, done.