Skip to main content

JSON Beautifier

Consistent indentation, every time.

Beautifying JSON re-serializes a document with consistent, predictable whitespace. Use it to tidy hand-edited configuration files, normalise output from different tools, or make a compact response readable before you commit it to a repository.

Array order is always preserved — only whitespace, and optionally key order, change.

Input JSON

Size: 0 BLines: 0

Beautified JSON

Paste valid JSON to see the output.
Size: 0 BLines: 0

Beautifying is re-serialization

Nothing here edits your text. The document is parsed into values and written out again from scratch, which is why the result is always consistent — and why a few things you may have cared about do not survive the trip.

SurvivesDoes not survive
Every key and valueComments — JSON has none, and the parse fails outright.
Key order, unless you sortTrailing commas.
Array order, always1.0 and 1e3, which come back as 1 and 1000.
Unicode content\u0041 escapes, which become the literal A.
Duplicate keys — the last one wins and the others vanish.
Integer precision past 2^53, silently.

If the input will not parse, nothing is produced at all — you get the error and its position instead. That is usually the faster path anyway: the validator explains exactly what is wrong.

Choosing an indentation

Four options, and the choice is mostly about who reads the file. Here is what each costs on the same 25-record document.

StyleSizevs minified
Minified1,319 bytes—
Tabs1,975 bytes+50%
2 spaces2,377 bytes+80%
4 spaces3,181 bytes+141%
8 spaces4,789 bytes+263%

Two spaces is the de facto standard for JSON and what almost every tool emits by default — package.json, composer.json, most API responses when they are pretty-printed. Tabs are the compact choice and let each reader pick their own visual width. Eight spaces exists mainly for deeply nested documents where you genuinely need the structure to shout; it gets expensive fast.

Sorting keys, and the order you actually get

Sorting is recursive over objects. Arrays are never reordered — their order is data, and changing it would change what the document means.

The sort is by string comparison, which is not quite alphabetical, and the difference surprises people:

Input, then sorted
{ "b": 1, "A": 2, "a": 3, "_x": 4, "10": 5, "2": 6, "Z": 7 }

{ "2": 6, "10": 5, "A": 2, "Z": 7, "_x": 4, "a": 3, "b": 1 }

Two things happened. Keys that look like integers came first, in numeric order — that is a JavaScript object rule, not a sorting choice, and "2" before "10" is the one part of this that matches intuition. The rest sorted by character code, which puts every uppercase letter before _, and _ before every lowercase letter. So Z sorts before _x, and _x before a.

This is worth knowing before you sort a file that other people will review, because "alphabetical" is not what they will see.

When to sort and when to leave it

  • Sort when you are comparing two documents by eye and the key order differs. Sorting both makes a text diff usable. (A structural diff does this for you without touching the files.)
  • Sort when generating a file that is checked into a repository and regenerated often — a stable order keeps the diffs small.
  • Do not sort package.json, tsconfig.json, or anything where convention puts name and version at the top. Sorting moves them into the middle and every reviewer notices.
  • Do not sort a file whose key order is documentation — configuration where related settings are deliberately grouped.

Sorting never changes what a parser sees. The risk is entirely social: a sorted file is a large diff, and a large diff hides the one line you actually changed.

Files that are nearly JSON

A lot of what people try to beautify is not JSON. tsconfig.json, .eslintrc.json and VS Code settings allow comments and trailing commas — that dialect is JSONC, and a strict parser rejects it by design.

  1. Strip comments and trailing commas, beautify, then put the comments back — workable for a one-off, tedious as a habit.
  2. Use a JSONC-aware formatter such as Prettier, which understands the dialect and preserves comments.
  3. Keep the file as-is and use this page only for the JSON parts you copy out of it.

The same applies to NDJSON and JSON Lines, where each line is a separate document. Beautify one line at a time, or the parse will fail on the second one.

Beautifier, formatter, prettifier

They are the same operation under different names, and this page and the JSON formatter share one implementation. The formatter page carries the deeper reference material on what re-serialization changes and how to handle very large documents; this page exists because "beautify" is what many people search for, and it should lead somewhere that answers the question.

Frequently asked questions

Is beautifying the same as formatting?
Yes — beautify, format, prettify and pretty-print all describe re-serializing a document with consistent indentation. This page and the JSON formatter run the same code.
Will sorting keys break my file?
Not for any parser — object key order carries no meaning in JSON. It can break conventions that people rely on, such as name and version at the top of package.json, and it produces a very large diff.
Why did my keys sort in that order?
Comparison is by character code, so uppercase letters come before _, which comes before lowercase. Keys that look like integers are listed first in numeric order, which is a JavaScript object rule rather than a sorting decision.
Can I beautify a file with comments in it?
Not directly — comments are not valid JSON and the parse fails. Files like tsconfig.json are JSONC; use a JSONC-aware formatter such as Prettier, or strip the comments first.
Which indentation should I use?
Two spaces unless you have a reason not to — it is what almost every tool emits and what most repositories already contain. Tabs are smaller and let readers choose their own width.
Does beautifying change my data?
The values stay the same, but re-serializing normalises how some of them are written: 1.0 becomes 1, \u0041 becomes A, and duplicate keys collapse to the last one. The formatter page lists every case.