ToolForge
How-toJSON

How to Format JSON

Formatting JSON means adding the whitespace a parser ignores and a human needs: one member per line, nesting shown by indentation. Minifying means taking all of it back out again.

Both are a round trip — parse the text into a structure, serialise the structure back out with different spacing — and that round trip changes a few things about your data that are worth knowing before you paste the result over the original.

Written by toolforge.websitePublished Last reviewed How we build and check these tools

Formatting in the browser

The document is parsed and re-serialised locally — no upload, which matters because the JSON you most need to read is usually an API response with a token or a customer record in it.

  1. Paste your JSON into the input panel.

    Minified, partially indented, or a wall of text from a network tab — the shape of the input does not matter as long as it parses.

  2. Choose Formatted.

    The output appears indented and syntax-highlighted, with one object member or array element per line.

  3. If it reports an error instead, read the position it names.

    The error message is the parser's own, and the tool converts the character position into a line number so you can find it.

  4. Use Tree for anything deeply nested.

    The tree view renders the document as collapsible nodes with each value's type, which beats scrolling when the nesting runs several levels deep.

  5. Copy or download the result.

    Or switch to Minified to get the compact form of the same document.

What formatting actually changes

This is the part most formatter pages skip. Formatting is not a cosmetic pass over the text — the document is parsed into a structure and a new document is written from that structure. Four things can differ between what you pasted and what you get back.

Duplicate keys collapse. A document containing the same key twice is legal JSON, and the parse keeps only one of them, so {"a": 1, "a": 2} formats to a single member. Data disappears, silently, and the output looks perfectly clean.

Numbers are normalised. 1.0 becomes 1, 1e3 becomes 1000, and -0 becomes 0. The value is the same as far as the format is concerned, because JSON has one number type; the text is not. And a number with more than about fifteen significant digits comes back rounded, which is the one case where formatting genuinely loses information.

Key order follows the parser. Members come out in whatever order the parser exposed, not necessarily the order you wrote. In practice JavaScript preserves insertion order for string keys, so this rarely surprises anyone — but it is not a guarantee the format makes.

Anything that was not JSON is gone. Comments, trailing commas and single quotes cannot survive a round trip, because the parse rejects them outright rather than tolerating them.

For a configuration file you are about to commit, that mostly means the formatted output is better than what you had. For a document you intend to compare byte-for-byte with something else, it means format both sides or neither.

Indentation, and why this tool gives you two spaces

The formatter here always indents with two spaces. That is not a missing feature so much as a deliberate absence of a decision — two spaces is what JSON.stringify(value, null, 2) produces, and it is the convention almost every JSON file in the wild follows, including package.json and tsconfig.json.

Where the convention differs:

ContextUsual indent
npm and most JS tooling2 spaces
.NET and Visual Studio output4 spaces
Files a human edits often2 spaces — nesting gets wide fast
Machine-to-machinenone, minified

If you need four spaces or tabs, the stringify tool exposes the spacing directly. In code, the third argument to JSON.stringify takes either a number of spaces or a literal string, so passing a tab character indents with tabs.

One thing indentation cannot fix is width. Deeply nested JSON becomes unreadable through sheer indentation before it becomes unreadable through lack of it, which is what the tree view is for.

When to minify

Minifying strips every byte of insignificant whitespace. On a heavily indented document it typically removes 15–30% of the characters — real, but less impressive than it sounds, because the actual answer to payload size is compression.

Gzip and Brotli are extremely good at repetitive whitespace. Once a response is compressed in transit, the difference between formatted and minified JSON is small, so if your server sets Content-Encoding correctly, minifying by hand is not where your bandwidth savings live.

Cases where minifying is genuinely worth doing:

  • Embedding JSON in something else — a data attribute, an environment variable, a URL parameter — where every character counts and newlines are awkward.
  • Storage at volume, where you are paying per byte and the same structure repeats millions of times.
  • A payload that is not compressed in transit, which is more common than it should be on internal services.

And the case against: never store minified JSON in version control if a human will ever have to review a change to it. A one-line file makes every diff the entire file, which is exactly the problem structural comparison exists to solve.

When formatting refuses

A formatter cannot format what it cannot parse, so an error here is a syntax error rather than a formatting failure. The message you get is the parser's own — something like Unexpected token } in JSON at position 412 — and the tool turns that character position into a line number.

Two shortcuts before you go hunting by eye:

Try the repair. The Fix button handles the four mistakes that account for most hand-written failures: single-quoted strings, unquoted keys, trailing commas, and a missing comma between adjacent objects or arrays. It is deliberately string-aware, so a URL like "http://example.com" or a time like "12:30" is left alone rather than being mangled by a global regex — the tool page explains the boundary of what it will touch.

Read the position, not the line. The character offset is exact; the line number is derived from it. If the message points at position 412 and your document is one long line, the line number will say 1 and the offset is the only useful information.

Every error message and its cause covers the rest, including the ones the repair cannot fix.

JSON Formatter

Format, validate, and beautify JSON online. It runs in your browser, so the files never leave your machine, and there is nothing to install or sign up for.

Try the JSON Formatter

Frequently asked questions

Is formatting JSON lossless?

As data, almost always; as text, not quite. The parsed values are preserved, but the round trip normalises number spellings, collapses duplicate keys and may reorder members. If you need the bytes to match, compare formatted output against formatted output.

Why does my formatted output show two spaces when I wanted four?

Because this formatter is fixed at two, matching JSON.stringify with an indent of 2 and the convention almost all JSON tooling follows. Use the stringify tool if you need to choose the spacing, or pass your own value as the third argument to JSON.stringify in code.

Does formatting make my JSON valid?

No — it requires your JSON to be valid before it can do anything. Formatting is a re-serialisation of a successful parse. The separate repair function fixes several common syntax mistakes, but that is a different operation from formatting.

How do I format a very large JSON file?

In-browser tools handle a few megabytes comfortably and then start to struggle, because the whole document has to be parsed and re-serialised in memory. For hundreds of megabytes use a streaming command-line tool such as jq, which processes without holding the entire structure at once.

The short version

Format for humans, minify for machines, and let compression handle the bytes on the wire. The one thing worth remembering is that formatting is a parse and a re-serialise rather than a text tidy-up: duplicate keys vanish, number spellings change, and a sixteen-digit integer comes back rounded. For everything you intend to keep, format once and commit the formatted version.

Tools that go with this

Each one is here for a specific reason rather than because it is in the same category.

JSON Stringify Tool

Convert JavaScript objects to JSON strings

Open Tool

The place to go when you need an indent other than two spaces, since it exposes the spacing directly.

JSON Parse Tool

Parse JSON strings into JavaScript objects

Open Tool

A leaner version of the same round trip when all you want is to see the parsed structure.

JSON Diff Comparator

Compare two JSON files and find differences

Open Tool

Formatting two files then eyeballing them is slower and less reliable than a structural comparison.

JSON Path Finder

Find and extract values from JSON using JSONPath

Open Tool

Better than scrolling when you know the key you want out of a large document.

Sources

  1. RFC 8259 (STD 90) — The JavaScript Object Notation (JSON) Data Interchange Format

    IETF · Standards organisation · published 1 December 2017 · accessed 7 September 2026

    Cited for: Whitespace being insignificant between tokens, and the interoperable precision limit behind number normalisation

  2. JSON.parse() — JavaScript reference

    MDN Web Docs · Technical documentation · accessed 7 September 2026

    Cited for: The parse behaviour the format-and-reserialise round trip depends on, and the errors it raises

About the author

toolforge.website

Builder and maintainer, ToolForge

Started ToolForge in May 2026 and has built and maintained it since, writing every tool on the site and the documentation that goes with each one.

How we research and check this content · About ToolForge

Part of

JSON

What JSON is, the syntax rules that actually trip people up, why a parser rejected your file, and how JSON compares with XML and YAML.

Browse all developer tools
Buy Me a Coffee