Skip to content

How to Format JSON: A Step-by-Step Guide for Developers

How to format JSON step by step — DevTools, JSON.stringify(null, 2) explained, python -m json.tool, jq ., and what a good online formatter should do.

Try it now: JSON Formatter & Validator Format, validate and minify JSON in your browser. Pinpoints syntax errors by line and column, sorts keys for clean diffs, and never uploads your data.

Why Raw JSON Is Hard to Read

An API response logged as a single line, or a minified config file someone committed by accident, is technically valid JSON — a parser has no trouble with it — but a human eye does. Nesting depth, which key belongs to which object, where an array ends: none of that is visible when everything sits on one line. Formatting (also called pretty-printingor beautifying) doesn't change any of that data. It re-inserts whitespace — newlines and indentation — according to the document's existing nesting structure, so the shape that was always there becomes visible. Nothing is added to or removed from the actual values; only the whitespace around them changes.

before → after
{"user":{"id":42,"name":"Ada Lovelace","roles":["admin","billing"]},"active":true}
↓
{
  "user": {
    "id": 42,
    "name": "Ada Lovelace",
    "roles": ["admin", "billing"]
  },
  "active": true
}

Below are four ways to do that formatting step, roughly in order of how quickly you'll reach for each one — a browser console for a quick look, the command line if you're already in a terminal, and a dedicated online tool when the JSON is large, malformed, or something you need to actually read closely.

Step 1: Format JSON in Your Browser's DevTools

If the JSON came from a network request, Chrome and Firefox DevTools already format it for you: open the Network tab, click the request, and the Response/Preview panel renders the body as a collapsible tree — no extra step needed. That covers the common case of “I just want to read this API response.”

For JSON you have as a raw string — copied from a log line, a file, or anywhere else — the Console panel is the fastest option. Paste this one line, with your JSON text in place of text:

console
JSON.stringify(JSON.parse(text), null, 2)

This is copied constantly without anyone explaining the two arguments after the value, so it's worth being explicit about what they do. JSON.parse(text) turns the raw string into an actual JavaScript value first — this is the step that would throw if the JSON were invalid. Then JSON.stringify takes three arguments: the value, a replacer, and a space argument. The nullin the middle position means “no replacer — include every key.” A replacer function or array lets you filter or transform keys as you serialize, but for plain formatting you always pass null. The final 2 is the indent width in spaces; pass 4 for four-space indentation, or the string "\t" for tabs instead of spaces.

Step 2: Format JSON from the Command Line

If you're already at a terminal — piping a curl response, or formatting a file before committing it — two tools cover almost every environment. Python ships json.tool in its standard library, so if Python is installed at all, this works with no extra install:

python -m json.tool
$ echo '{"id":42,"active":true}' | python -m json.tool
{
    "id": 42,
    "active": true
}

If jqis available — it's a dedicated JSON processor worth installing anyway, since it also queries and filters JSON — the equivalent is even shorter: jq . pretty-prints its input with the identity filter.

jq .
$ echo '{"id":42,"active":true}' | jq .
{
  "id": 42,
  "active": true
}

Both read from stdin, so they drop straight into a pipeline: curl https://api.example.com/users/42 | jq . formats a response as it arrives, with no intermediate file.

Step 3: Use a Dedicated Online JSON Formatter

A console one-liner or a CLI pipe is fine for JSON that's already valid and reasonably small. Once the document gets long, deeply nested, or — the actual common case — broken, a purpose-built online formatter earns its keep in three ways a plain JSON.stringifycall doesn't give you:

  • Syntax highlighting. Keys, strings, numbers and booleans in distinct colors make a large document scannable instead of a wall of monochrome text.
  • Collapsible nodes.Being able to fold a large nested object or array down to a single line, then expand only the branch you're actually debugging, matters once a document is more than a screen or two long.
  • A precise error location for malformed input.This is the part that matters most. A trailing comma, a stray single quote, an unquoted key — a good formatter parses the document and, when that fails, reports the exact line and column where it broke, rather than failing silently or throwing a generic “unexpected token” with no location at all.
a syntax error, located
{
  "name": "Ada",
  "roles": ["admin",]
}
→ Unexpected ] — trailing comma before it (line 3, column 20)

GenKitLab's JSON Formatter works this way: it parses your input first, so a syntax error is pinpointed by line and column instead of producing a vague failure, and everything runs client-side — nothing you paste is uploaded anywhere.

A Note on Indentation Width

There's no single correct indent width — it's a convention, not a rule enforced by the JSON spec. Two spaces is the most common default across web-development tooling (Prettier, most editors' JSON defaults, and the examples above all use it), but plenty of established codebases use four spaces, and some use tabs. Whichever width you pick, formatting only changes whitespace — it never reorders keys or changes a value's type, unless the specific tool you're using explicitly offers key-sorting as a separate, opt-in option. If a formatter changed your key order without you asking it to, that's a bug in the tool, not a side effect of formatting itself.

Formatting vs. Minifying vs. Validating

These three terms get used loosely, but they describe three different operations on the same document:

  • Formatting (pretty-printing) adds whitespace for readability, as covered above.
  • Minifying is the exact reverse — stripping all insignificant whitespace back out to shrink the byte size, typically before sending a payload over the wire or storing it in a size-constrained field. See the JSON Minifier guide for that operation in detail.
  • Validatingis a different concern entirely: checking that a document is syntactically correct JSON (or, with a schema, that it matches an expected shape) — it doesn't touch whitespace at all. A document can be perfectly valid and still minified, or invalid and still “formatted” in the sense of having indentation. See Is My JSON Valid? for the difference between a syntax error and a schema mismatch.

In practice you often want the same tool to handle both directions — format to debug, minify to ship — which is exactly what JSON Formatter does.

Frequently asked questions

How do I format JSON online?

Paste it into a browser-based JSON formatter. A good one parses the document first — so a syntax error is reported with an exact line and column instead of failing silently — then re-indents it with syntax highlighting and collapsible nested nodes. GenKitLab's JSON Formatter does this entirely client-side, so nothing you paste is uploaded.

What does JSON.stringify's null and 2 argument actually do?

In JSON.stringify(value, null, 2), the second argument is a replacer that lets you filter or transform keys during serialization — null means include everything, unchanged. The third argument, 2, is the indent width in spaces used for pretty-printing; pass a different number for a wider indent, or the string "\t" to use tabs instead of spaces.

How do I pretty print JSON from the command line?

Pipe it through python -m json.tool (built into Python's standard library, no install needed) or jq . (a dedicated JSON processor). Both read from stdin, so they work in a pipeline: curl https://api.example.com/data | jq . formats a response as it arrives.

What's the standard JSON indentation width?

Two spaces is the most common convention in web development and is what most editors and formatters default to, but it's a convention, not a JSON spec requirement — plenty of codebases use four spaces or tabs instead. Any consistent width is valid; pick one and apply it consistently within a project.

Does formatting JSON change the data or the key order?

No. Formatting only adds or removes whitespace between tokens — it never changes a value's type, adds or removes keys, or reorders them. If a tool changes key order, that's a separate, explicit sorting feature, not a side effect of formatting.

What's the difference between formatting, minifying, and validating JSON?

Formatting adds whitespace for readability; minifying is the reverse operation, stripping whitespace to shrink byte size; validating checks whether a document is syntactically correct (or matches a schema) and doesn't touch whitespace at all. A document can be valid and minified, or invalid and indented — the three are independent.

Why does my JSON fail to format with a generic 'unexpected token' error?

That's usually a tool limitation, not a description of the actual problem. The most common real causes are a trailing comma before a closing bracket, single quotes instead of double quotes around strings and keys, or an unquoted key. A formatter that parses before beautifying reports the exact line and column of the offending character instead of a generic message.

Last updated