← Back to Blog

How to Format JSON: A Complete Guide to Pretty-Printing and Validation

March 25, 2026 · 6 min read

You've probably stared at a wall of minified JSON trying to find a missing bracket. I know I have. JSON (JavaScript Object Notation) is the default data interchange format for web APIs, config files, and database exports, but raw JSON from an API response or a log file is often compressed into a single unreadable line. Formatting it properly is the first step to understanding what the data actually says.

What Is JSON?

JSON is a lightweight text format for structured data. It uses key-value pairs and ordered lists. It was derived from JavaScript syntax but is language-independent, so virtually every programming language can parse and generate it out of the box. A typical JSON object looks like this:

{
  "name": "Alice",
  "age": 30,
  "languages": ["Python", "Go", "Rust"],
  "address": {
    "city": "Berlin",
    "zip": "10115"
  }
}

When this same object comes back from an API endpoint, it usually arrives minified:

{"name":"Alice","age":30,"languages":["Python","Go","Rust"],"address":{"city":"Berlin","zip":"10115"}}

Both are identical data. The first version is for humans; the second is for the wire. A JSON formatter converts between the two.

Why Format JSON?

Pretty-printing is not cosmetic. It is a debugging tool. Indentation reveals nesting depth at a glance, so you can immediately see that address is a child of the top-level object. A trailing comma or a missing bracket is nearly impossible to spot in a minified string, but in formatted JSON those errors jump right out.

Formatted JSON also makes diffing practical. Two minified payloads with a single field change show up as one giant modified line. Format them first, and you get meaningful line-by-line diffs. And if you are writing API docs or sharing example responses with your team, formatted JSON is just the expected standard.

Common JSON Syntax Errors (and How to Fix Them)

JSON's grammar is simple, but the strictness catches people off guard. These are the errors I see developers hit most often.

1. Trailing Commas

JavaScript allows a comma after the last element in an array or object. JSON does not. This is invalid:

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

Remove the comma after 30 and it parses correctly.

2. Single Quotes

JSON requires double quotes around all strings and keys. Using single quotes like {'name': 'Alice'} produces a parse error. Always use "name", not 'name'.

3. Unquoted Keys

In JavaScript you can write {name: "Alice"}, but JSON demands {"name": "Alice"}. Every key must be a double-quoted string.

4. Comments

JSON has no comment syntax. Neither // line comment nor /* block comment */ is valid. If you need comments in config files, look at JSON5 or JSONC (VS Code supports JSONC), or just use YAML or TOML instead.

5. Numeric Formats

Leading zeros are not allowed (007 is invalid, 7 is fine). Hex literals like 0xFF are not valid JSON either. Infinity and NaN get rejected too. Use null or a string representation instead.

How to Format JSON

Command Line

If you have Python installed, python3 -m json.tool reads from stdin and writes formatted JSON to stdout:

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

With jq installed, you can do a lot more:

curl -s https://api.example.com/data | jq '.'

In Your Editor

VS Code: select the JSON, open the command palette (Ctrl+Shift+P), and run "Format Document." IntelliJ-based IDEs: Ctrl+Alt+L. Vim users can pipe through jq with :%!jq ..

Online

Sometimes you are debugging a webhook payload from a Slack notification or helping a teammate over screen-share, and opening an IDE is overkill. An online formatter gets you from paste to pretty-print in under a second, no install needed.

Paste your JSON and see it formatted instantly, with syntax validation and error highlighting.

Try the Free JSON Formatter

What Makes a Good JSON Formatter?

Not all formatters are equal. A good one tells you exactly where a syntax error is, not just "invalid JSON." It lets you pick your indentation style (two spaces, four spaces, tabs) and also minify in the other direction, which is useful when you need to embed JSON in a config or a URL. Privacy matters too: your JSON might contain API keys or user data, so pick a tool that processes everything client-side with no server round-trip.

Beyond Formatting: Related JSON Operations

Once your JSON is formatted, you might need to do more with it. A few tools that pair well with a JSON formatter:

Frequently Asked Questions

What does it mean to pretty-print JSON?

Pretty-printing JSON means adding line breaks, indentation, and spacing to a JSON string so that its structure is easy to read. Minified JSON packs everything onto one line to save bytes, while pretty-printed JSON uses whitespace to show the nesting of objects and arrays.

Why is my JSON invalid?

The most common reasons are trailing commas after the last item, single quotes instead of double quotes, unquoted property names, comments (JSON does not support them), and missing or extra brackets. A JSON formatter with validation will point you to the exact line where the error occurs.

Is there a difference between JSON formatting and JSON validation?

Yes. Validation checks whether the string is syntactically correct per RFC 8259. Formatting takes valid JSON and adds indentation and line breaks for readability. Most online tools do both simultaneously. They validate the input and output a formatted version if it passes.