# JSON Formatter & Validator

> Validate, pretty-print, minify, and sort JSON. Parse errors point to the exact line, column, and path; duplicate keys are flagged; and large numbers are preserved exactly. Runs entirely in your browser.

- Tool: https://ronutz.com/en/tools/json-formatter
- Family: Encoding & data

---

## What it does

Paste JSON and this tool validates it, then pretty-prints, minifies, or sorts it. What sets it apart from a plain `JSON.parse` is what it tells you when something is wrong or unusual: a parse error names the exact line, column, and path to the problem; repeated object keys are flagged; and very large or very precise numbers are preserved exactly rather than being rounded. Everything runs in your browser.

## A parser built for diagnostics

The engine is a hand-written parser that tracks position as it reads, rather than relying on the JavaScript runtime's built-in parse. That buys three things that matter in practice:

- **Precise errors.** When parsing fails, you get the line, the column, the byte offset, a plain-language message, and the JSON Pointer (RFC 6901) path to the location, instead of a single engine-dependent `SyntaxError` with no path.
- **Duplicate-key detection.** The JSON grammar technically permits an object to repeat a key, and `JSON.parse` silently keeps only the last one. That silent behavior hides real bugs, so this engine reports every duplicate it finds, by path.
- **Exact large numbers.** JSON numbers have no size limit, but a JavaScript number is a 64-bit float and loses precision above 2^53. The formatter preserves the digits you wrote, so a 20-digit id or a high-precision decimal is not quietly rounded.

## Format, minify, and sort

Once the JSON is valid you can pretty-print it with consistent indentation, minify it to the smallest single-line form, or sort object keys so that two documents which differ only in key order line up for comparison. Sorting is by key and does not reorder arrays, whose order is significant in JSON.

## Worked examples

- `{"b":1,"a":2}` sorted by key becomes `{"a":2,"b":1}`.
- `{ "a": 1 }` minified becomes `{"a":1}`, with the whitespace removed.
- `{"id":10000000000000001}` keeps all its digits, whereas a round-trip through a 64-bit float would turn it into `10000000000000000`.
- `{"a":1,"a":2}` is flagged for the duplicate key `a`; a standard parser would silently keep `2`.

## Using it

Paste a JSON document and choose to format, minify, or sort it. If it does not parse, the error points at the exact spot; if it parses but has duplicate keys or oversized numbers, those are called out. The tool is a pure function of its input, so the same document always gives the same result.

## Standards and references

- [RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259) - the JSON grammar: value types, structural characters, strings, and numbers
- [ECMA-404 - The JSON Data Interchange Syntax](https://ecma-international.org/publications-and-standards/standards/ecma-404/) - the formal JSON syntax, equivalent to RFC 8259
- [RFC 6901 - JavaScript Object Notation (JSON) Pointer](https://www.rfc-editor.org/rfc/rfc6901) - the pointer path syntax used to locate errors and duplicate keys

## Related reading

- [Duplicate Keys in JSON: Legal, Dangerous, and Worth Catching](https://ronutz.com/en/learn/json-duplicate-keys.md): JSON syntax allows the same key to appear more than once in an object, but the specification does not say what that means. Different parsers resolve it differently, which makes duplicate keys a quiet source of bugs and even security issues.
- [Formatting, Minifying, and Canonical JSON](https://ronutz.com/en/learn/json-formatting-and-canonical.md): Whitespace does not change what JSON means, so pretty-printed and minified JSON are the same data. But when JSON is signed or hashed, the exact bytes matter, and that is where canonical JSON comes in: a deterministic way to serialize the same data to exactly the same string every time.
- [JSON Numbers and the Precision Trap](https://ronutz.com/en/learn/json-numbers-and-precision.md): JSON puts no limit on the size or precision of a number, but most parsers quietly convert every number to a 64-bit float. That mismatch silently corrupts large integers and exact decimals, which is why a formatter should preserve the original digits.
- [JSON String Escapes and Unicode](https://ronutz.com/en/learn/json-string-escapes.md): Inside a JSON string, a few characters must be written as escapes, and any character at all can be written as \uXXXX. The rules are small but strict, and the one that catches people is how characters beyond the basic plane, like emoji, need a surrogate pair.
- [The JSON Grammar: Six Types and a Few Strict Rules](https://ronutz.com/en/learn/json-grammar.md): JSON is smaller than it looks. The whole format is six value types and a handful of structural characters, governed by rules that are stricter than most people remember: no comments, no trailing commas, and keys that must be quoted strings.
- [Trailing Commas, Comments, and the JSON5 Family](https://ronutz.com/en/learn/json-comments-and-trailing-commas.md): Strict JSON has no comments and no trailing commas, which surprises people whose editor accepts both. The reason is that JSON is a minimal interchange format, and the tolerant variants (JSONC, JSON5) are separate things. Knowing which is which avoids config files that break in another tool.
