Guide

How to format JSON

Formatting JSON parses the document and serializes it again with readable indentation. It does not preserve every byte: duplicate keys, lexical number spellings, escape choices and other source-level details may be normalized or lost. The resulting JSON represents the parser's value, so use the validator first when you need to inspect duplicate keys or other source-level problems.

Most people arrive at a JSON formatter for one of two reasons: a single-line API response is unreadable, or a parser has rejected a file and the error message was unhelpful. The first is a one-click job. The second is where a formatter earns its place, because the position of the error tells you far more than its description.

Open the JSON Formatter

Use this when


  • An API returned one long line and you need to find a specific field in it
  • A parser said "unexpected token" and you need to know where
  • A config file has been edited by several people with different indentation
  • You want to diff two JSON documents without whitespace noise in the diff
  • A file needs consistent key ordering before being committed

Reading a syntax error properly


A JSON error reports the first position where the document stopped being valid, not necessarily where the mistake is. Those are usually the same place, but the important exception is a missing closing bracket or brace: the parser happily continues to the end of the file looking for it, so the error lands on the last line even though the real problem is wherever the structure was left open.

When the reported line looks fine, read the error as "everything up to here parsed, and then the document ran out". Then look for the unclosed structure above it. Formatting the part that does parse is often enough to make the missing bracket obvious, because the indentation will not return to the left margin where you expect it to.

The other common surprise is a trailing comma. JSON does not allow one, though almost every language that reads JSON does allow it in its own object literals, so the habit is easy to carry over. The error appears at the closing bracket rather than at the comma, since a comma is legal right up until it turns out nothing follows it.

Choosing an indentation width


Two spaces is the prevailing convention and the one to pick if there is no reason to do otherwise. It is what npm, most linters and most code formatters produce, so it minimises diff noise when a file is shared.

Four spaces reads more clearly for shallow documents but wastes horizontal space quickly once nesting passes three or four levels, which is common in API responses. Tabs let each reader choose their own width, which is genuinely useful for accessibility, but not every tool renders them identically.

Minifying, removing all optional whitespace, is a different goal. It is worth doing for data being transmitted or stored, where the saving is real, and worth avoiding for anything a person is expected to read or a version control system is expected to diff.

What sorting keys does and does not change


JSON object keys are unordered by specification: two documents with the same keys in a different order represent the same object. In practice order is preserved by almost every parser and serialiser, and people rely on it, so sorting is a visible change even though it is not a semantic one.

Sorting is most useful before comparing two documents. Two API responses that differ only in key order will produce a large, meaningless diff; sorting both first reduces it to the differences that matter.

It is worth being careful with configuration files, where a human-chosen order often carries meaning, the important settings first, related settings grouped. Alphabetising that is technically lossless and practically a regression.

Duplicate keys, and why they vanish


RFC 8259 recommends unique names within an object and warns that implementations handle duplicates differently. JavaScript JSON.parse keeps the last value. This formatter therefore discards earlier duplicate members without a duplicate warning; other parsers may reject them instead.

Check the original source with the JSON Validator before formatting. Its duplicate-key scan catches ordinary repeated names in the same object, but escaped names can cause missed matches or false positives. Inspect the source and use a strict decoded-key validator when uniqueness is a security requirement.

Large numbers and precision limits


These browser tools parse JSON numbers into JavaScript IEEE 754 doubles. Not every integer beyond the safe-integer limit of 9,007,199,254,740,991 is representable; high-precision decimals can round too. Other JSON implementations can use different numeric representations, so this is an implementation limit rather than a rule of JSON syntax.

For example, 9007199254740993 becomes 9007199254740992 after JavaScript parsing. A formatter that preserves numeric tokens or uses arbitrary-precision values can behave differently, but this formatter cannot restore digits already lost during parsing. Send exact identifiers as strings or use a precision-preserving workflow.

Common problems


Each of these is something that actually happens, with the cause rather than a generic suggestion to check your input.

The error points at the last line, which looks correct

Cause
A bracket or brace was left unclosed earlier, so the parser read to the end of the file looking for it.
Fix
Format what you have and look at the indentation. It will not return to the left margin where you expect. The unclosed structure is the last one still indented.

A field you know you set is missing from the output

Cause
The key appears twice in the document, and the parser kept the last one.
Fix
Run the validator, which reports duplicate keys explicitly instead of silently discarding them.

An ID comes out with different digits at the end

Cause
The value is larger than 2^53 and cannot be represented exactly as a double.
Fix
Change the producing API to send the value as a string. No formatter can recover the lost precision after the fact.

The whole document is rejected and it came from a shell

Cause
Single quotes. JSON strings require double quotes, and a shell often converts or strips them.
Fix
Quote the whole payload in single quotes and use double quotes inside, or write it to a file and pass the file.

Questions


Does formatting change my data?
For ordinary JSON values, formatting preserves the parsed meaning while changing the serialized layout. It is not byte-preserving: parsing can discard an earlier duplicate key and normalize number or escape spellings before the formatter writes the result. Use the JSON Validator when source-level duplicate keys matter; sorting keys also changes the visible order.
Is my JSON uploaded anywhere?
No. The parsing and formatting happen in JavaScript running in your browser, inside a Web Worker. You can confirm it by opening your browser developer tools, switching to the Network tab, and formatting something, no request carries your JSON input or formatted output, because formatting happens locally rather than through an upload endpoint.
Can I format JSON with comments?
Comments are not valid JSON, so a strict parser rejects them. Several dialects allow them, JSON5, JSONC, and the format used by many editor configuration files. If your document has comments and needs to stay that way, YAML is usually the better target, since it supports comments natively.
What is the largest file I can format?
The practical limit is about 12 MB, which is where in-browser processing stops being fast enough to feel instant. Above roughly 200,000 characters the live preview pauses and you press the button instead, so a large paste does not re-parse on every keystroke.