JSON Formatter

Pretty-print or minify JSON and pinpoint syntax errors

About JSON Formatter

This tool reads what you paste with the browser's built-in JSON.parse and writes it back out with JSON.stringify. The output is therefore not a tidied copy of your text; it is a fresh serialization of the value the engine understood. That is why 0.1000 comes back as 0.1, 1E3 as 1000, and \u00e9 as é.

JSON's grammar is tiny, but text written by hand or copied from a JavaScript object literal picks up non-standard syntax easily. Below is what the specs (RFC 8259 and ECMA-404) actually allow, where people usually trip, and the cases where parsing quietly changes your data.

Syntax people use that the spec rejects

Under RFC 8259 a JSON text is any single value. Older specs required the top level to be an object or array; today a bare "hello", 42 or null is valid JSON, and this tool accepts it. Everything in the list below, however, is an error.

  • Trailing commas: {"a": 1,} and [1,2,3,]
  • Single-quoted strings and unquoted keys: {'a': 1}, {a: 1}
  • Comments of any kind, // or /* */
  • NaN, Infinity and -Infinity. The RFC says numeric values that cannot be represented in the grammar, such as Infinity and NaN, are not permitted. Going the other way, JavaScript's JSON.stringify({x: NaN}) gives {"x":null} without any warning, so the value simply disappears.
  • Leading zeros: 01 fails; 0.5 is fine but .5 is not.
  • Only four characters count as whitespace: space, tab, line feed and carriage return. A byte order mark (U+FEFF) at the start of a file is not one of them, so JSON.parse fails on the very first character. RFC 8259 says implementations MUST NOT add a BOM to networked JSON. If a file saved as "UTF-8 with BOM" errors out at position 0, this is why.

Files that allow comments are not JSON

VS Code opens settings.json, tasks.json and launch.json in "JSON with Comments" (jsonc) mode, where // and /* */ comments work and trailing commas only produce a warning. JSON5 is a separate format that goes further, allowing single quotes, unquoted keys and Infinity. Pasting such a file here and getting an error is the expected result: strip the comments and trailing commas first, or use a parser built for that format.

Three ways parsing changes your data

Duplicate keys. The spec only says names SHOULD be unique and that when they are not, "the behavior of software that receives such an object is unpredictable." JavaScript keeps the last one, so {"a":1,"a":2} formats as {"a": 2} and the first value vanishes. Parsers in other languages may keep the first value or refuse the document.

Large integers. JavaScript numbers are IEEE 754 doubles, and RFC 8259 notes that only integers in the range -(2^53-1) to 2^53-1 (9007199254740991) are interoperable in the sense that implementations agree exactly on their values. A 64-bit ID beyond that loses its last digits. This is why the Twitter (now X) API sent every tweet ID twice, as a number id and a string id_str, and its documentation told JSON consumers to use id_str. JSON.parse rounds such values without any error, so this tool scans the input for integers beyond 2^53 and shows a warning next to the result.

Key order. Even with sorting turned off, integer-like keys such as "2" and "10" move to the front in ascending numeric order, because that is how JavaScript objects enumerate properties. With sorting on, the tool writes the output itself instead of relying on object order, so every key — integer-like ones included — is ordered by character code: uppercase before lowercase, and "10" before "2". Array order is never touched.

Reading the error message

The message is the engine's own English text. Recent Chrome and Edge (V8) versions write things like "Expected ',' or '}' after property value in JSON at position 23 (line 4 column 3)". Firefox uses the form "JSON.parse: expected ',' or '}' after property value in object at line 4 column 3 of the JSON data". The tool looks for "line L column C" or "position N" in the text and converts it to a line and column. Some messages carry no position at all, for example V8's "Unexpected token ']', "[1,2,3,]" is not valid JSON", and then no location is shown.

The reported location is where the parser gave up, not where you made the mistake. Drop a comma at the end of line 3 and the error points at the key on line 4. Look just before the marked spot first.

How much does minifying save?

Measured on an array of 1,000 objects with three fields (id, name, active), using Node.js zlib at default settings: pretty-printed with 2 spaces it is 67,282 bytes, minified 43,281 bytes, a 36% cut in raw size. After gzip the two are 5,371 and 5,106 bytes, about 5% apart; with Brotli, 2,493 and 2,245 bytes. If your server already compresses responses, stripping whitespace buys little. It matters more for storage, for paths with no transfer compression, and when JSON has to fit on one log line.

Worked examples

A missing comma

Input
{ "a": 1, "b": 2 "c": 3 }
Output
Syntax error: Expected ',' or '}' after property value in JSON at position 23 (line 4 column 3), at line 4, column 3 (Chrome)

The mistake is at the end of line 3, after the 2, but the parser reports it at "c" on line 4.

A 64-bit ID that changes

Input
{"id":1050118621198921728,"id_str":"1050118621198921728"}
Output
{ "id": 1050118621198921700, "id_str": "1050118621198921728" }

The numeric id comes back with different trailing digits; the string id_str is intact. JSON.parse raises no error (this tool adds a warning), which is why large IDs should travel as strings.

Sorting keys

Input
{"b":1,"10":2,"2":3,"a":4}
Output
{ "10": 2, "2": 3, "a": 4, "b": 1 }

Sorting compares keys as strings, so "10" comes before "2" (1 sorts before 2). With sorting off, integer-like keys follow the JavaScript object rule instead and lead in numeric order: "2", then "10".

How to use it

  1. Paste your JSON into the input box.
  2. Pick an indent (2 spaces, 4 spaces or tab) and decide whether to sort keys.
  3. Press Format or Minify. On an error, check the text just before the reported line and column.

Frequently asked questions

Why does my VS Code settings file fail here?

It almost certainly contains comments or trailing commas. VS Code reads it as JSON with Comments, which is looser than standard JSON. Remove those and it will parse.

Can I paste a JavaScript object?

Unquoted keys like {a: 1}, single quotes, undefined and functions are not JSON. Run JSON.stringify(yourObject) in the browser console and paste the result.

Why did 0.1000 turn into 0.1?

The tool re-serializes the parsed number. The value is the same, but the original spelling is not kept. If the exact digits matter, as with prices, store them as strings.

My JSON has a duplicate key but no error appears.

JSON.parse does not treat duplicates as an error; it keeps the last value. The earlier value is gone from the output, so compare with the original.

Do error messages differ between browsers?

Yes. The wording and position format come from the engine, so Chrome and Firefox read differently, and some messages give no position.

Sources

Last reviewed:

Privacy and how data is handled