How to Format JSON

Formatting JSON adds or removes whitespace without changing a single value. Pretty printing makes it readable for humans, minifying makes it smaller for machines, and both require the JSON to be valid first.

What formatting changes

Only whitespace outside strings: spaces, tabs and line breaks between tokens. The keys, values, order of keys and types are untouched.

That guarantee is why formatting is safe to run on any valid document. If a formatter appears to change a value, the input was not valid JSON to begin with.

Paste JSON to format, minify and validate it.

Open the JSON Formatter and Validator →

Pretty printing

Pretty printing puts each key on its own line and indents nested objects. Two spaces per level is the most common convention, with four used in some codebases and tabs in others.

The result is far easier to read and to diff in version control, at the cost of a file several times larger.

Minifying

Minifying removes all optional whitespace, leaving the smallest valid representation. A 40 KB pretty-printed file can drop to 25 KB or less, which matters for network payloads.

Never hand-edit minified JSON. Format it first, edit, then minify again if size matters.

What a formatter also checks

Most formatters validate as they format, so they catch a missing comma, a trailing comma, an unquoted key or a single quote used instead of a double quote.

That validation is the real value for most people. The indentation is cosmetic; the error message is not.

Key order

JSON objects are unordered by specification, so a formatter may keep the original order or sort the keys. Both are valid.

Sorting keys makes diffs stable, which is useful when comparing two versions of the same document. Keeping the original order is better when the order carries meaning to a reader.

Numbers and precision

JSON numbers have no declared precision. A large integer can lose precision when parsed by a system that stores it as a floating-point number, and a formatter that round-trips through such a parser can change the value.

For identifiers such as database keys or account numbers, storing them as strings avoids the problem entirely.

Unicode and escapes

A formatter may convert escaped characters such as \u00e9 into the character itself, or leave them escaped. Both are equivalent and both are valid.

If the output will be embedded in HTML or a script tag, keep escapes where they prevent parsing problems.

Common mistakes

Formatting in code

Every language has a JSON library with a dump or stringify function that takes an indent argument. In Python it is json.dumps with indent=2. In JavaScript it is JSON.stringify with a third argument.

Use the library rather than writing a formatter. Hand-written formatting code eventually mangles a string containing a brace.

When formatting matters most

Reviewing a change, debugging an API response, writing a configuration file by hand and preparing an example for documentation all benefit from pretty printing.

For anything sent over a network or stored at scale, minify and compress instead.

A quick workflow

Paste the JSON, format it, fix the errors the formatter reports one at a time, then format again until it is clean. Read the top level keys to confirm the structure is what you expect before looking at values.

Formatting before a review

A formatted JSON diff shows one changed line. A minified one shows the entire payload as a single changed line, which tells a reviewer nothing.

Format before committing, and let the build step minify for production if size matters.

Common reasons formatting fails

Most failures come from a few mistakes: a trailing comma after the last item, single quotes instead of double quotes, or an unquoted key. JSON allows none of these.

Comments are not part of JSON either. If you copied a snippet from documentation with comments, remove them before parsing.

When the error points to a position, look at the character just before it. The real mistake is often one token earlier than where the parser gave up.

Frequently asked questions

Does formatting change the data?

No. It changes only whitespace outside strings.

Should I use two or four spaces?

Two is the most common convention. Consistency within a project matters more than the number.

Can a formatter repair invalid JSON?

No. It can tell you where the error is, but the fix is manual.

Sources

More free tools

Related guides