jsonpost
JSONFormatting

How to Format JSON (and Why It Matters)

A practical guide to beautifying, indenting, and minifying JSON — and when to use each, with tips for debugging messy API responses.

JSONPost··5 min read
How to Format JSON

JSON is everywhere — APIs, config files, logs, message queues — but raw JSON is often delivered as a single unreadable line. Formatting (also called beautifying or pretty-printing) adds indentation and line breaks so humans can actually read it. It's one of those small tasks developers do dozens of times a day, and doing it well saves real time when you're debugging.

This guide covers what formatting actually does, when to beautify versus minify, how to format JSON in code, and how to deal with the messy, almost-valid JSON you get from the real world.

What formatting actually does

Formatting doesn't change your data — it only changes the whitespace between tokens. A typical API response arrives compact, like this:

{"id":42,"name":"Ada Lovelace","roles":["admin","editor"],"active":true}

Beautified, the exact same data becomes scannable:

{
  "id": 42,
  "name": "Ada Lovelace",
  "roles": ["admin", "editor"],
  "active": true
}

The keys, values, and structure are identical; only the spacing differs. That's why you can safely beautify a production payload to read it and minify it again before sending it back — nothing of substance is lost.

Beautify vs. minify

There are two opposite operations, and each has its place:

  • Beautify adds whitespace for readability. Use it while debugging, reviewing data, or writing documentation.
  • Minify strips every unnecessary space and line break to shrink the payload. Use it in production to save bandwidth and reduce response time.

The size difference is meaningful at scale. The compact object above is 71 bytes; the beautified version is roughly 110 bytes. On a single request that's nothing, but across millions of API calls the whitespace adds up to real bandwidth. The rule of thumb: minify what machines read, beautify what humans read.

Try the JSON Formatter to beautify, and the JSON Minifier to compress and see the exact byte savings.

Formatting in code

Most languages can format JSON without any external tool. In JavaScript, JSON.stringify takes a third argument that controls indentation:

const data = { id: 42, name: "Ada", roles: ["admin"] };

// Compact (the default)
JSON.stringify(data);
// {"id":42,"name":"Ada","roles":["admin"]}

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

// Pretty-printed with tabs
JSON.stringify(data, null, "\t");

On the command line, two tools are everywhere. jq formats by default, and Python ships a JSON formatter as a module:

# Using jq
echo '{"id":42,"name":"Ada"}' | jq

# Using Python's built-in tool
echo '{"id":42,"name":"Ada"}' | python3 -m json.tool

These are perfect for piping a curl response straight into a readable shape:

curl -s https://api.example.com/users/42 | jq

When you don't have a terminal handy — or you want a tree view and instant error checking — the browser-based JSON Formatter does the same job with no setup.

Choosing an indentation

Indentation is mostly a matter of team convention, but the common choices each have a rationale:

  • Two spaces is the most popular and what most editors default to. It keeps deeply nested data from drifting too far to the right.
  • Four spaces reads well in documentation and blog posts where vertical rhythm matters more than horizontal space.
  • Tabs respect each developer's editor settings, so everyone can view the file at their preferred width.

The specific choice matters less than consistency. A stable, predictable format makes diffs in version control dramatically easier to read, because only the lines you actually changed show up as changes.

Sorting keys for cleaner diffs

A related trick: sort object keys alphabetically. JSON objects are unordered, so two equivalent documents can list keys in different orders and produce a noisy diff even when nothing meaningful changed. Normalizing the key order — for example with the JSON Sorter — gives you a canonical form that diffs cleanly:

{
  "active": true,
  "id": 42,
  "name": "Ada Lovelace",
  "roles": ["admin", "editor"]
}

Combine "sort keys, then format with 2 spaces" as a pre-commit step and your JSON fixtures stop generating spurious diffs.

Fixing messy JSON

Real-world JSON is frequently almost valid. The most common reasons a document won't parse are:

  • a trailing comma after the last item in an object or array,
  • single quotes instead of the double quotes JSON requires,
  • unquoted keys copied from a JavaScript object literal, and
  • comments, which standard JSON does not allow.

Here's a snippet that looks fine but will not parse:

{
  name: 'jsonpost',
  tags: ['json', 'tools',],
  // a trailing comma and single quotes
}

You have two options. The JSON Fixer repairs these issues automatically — converting quotes, removing trailing commas and comments, and quoting keys — to give you valid JSON in one step. When you'd rather understand the error yourself, the JSON Validator pinpoints the exact line and column of the first syntax problem so you can fix it by hand.

Reading parser errors

When JSON.parse throws, the message usually includes a position, and a good validator translates that into a line and column:

Unexpected token } in JSON at position 58 (line 4, column 1)

That tells you the parser reached a closing brace it didn't expect — almost always a sign of a trailing comma or a missing value on the line above. Learning to read these messages turns a frustrating "invalid JSON" into a five-second fix.

Working with large files

For big or deeply nested documents, a flat wall of text isn't enough. A tree view lets you collapse sections and focus on the part you care about, and features like search and copy-path help you navigate. The JSON Viewer gives you an expandable tree and copyable paths, which is far easier than scrolling through thousands of formatted lines.

A note on privacy

Every tool on JSONPost runs entirely in your browser. Your data is never uploaded to a server, so it's safe to format and inspect production payloads, access tokens, and private API responses without anything leaving your device.

Wrapping up

Formatting JSON is simple, but doing it deliberately — beautifying for humans, minifying for machines, keeping indentation consistent, and sorting keys for clean diffs — makes everyday development noticeably smoother. Next time you paste a tangled one-liner, run it through the JSON Formatter to read it, the JSON Minifier to ship it, and the JSON Fixer when it refuses to parse.

Keep reading