JSON formatter
Paste JSON and it is reformatted, or squeezed onto a single line.
Result
Paste JSON in the field above.
How it works
JSON is a text format for structured data, built from objects with named fields, arrays, strings, numbers, true, false and null.
Formatting only changes the whitespace. The data is identical before and after, so you can safely reformat a file before reading it.
Minifying removes every unnecessary space and line break, which typically cuts twenty to thirty per cent off indented JSON.
When the text cannot be parsed, the message comes from the browser’s own parser and usually names the character that was unexpected. The commonest causes are a trailing comma before a closing brace and the wrong kind of quotation mark.
Comments are not allowed in JSON, and field names must be in straight double quotes. That is the most common difference from how objects are written in JavaScript.
How the tool formats
The tool does two things in turn. First the text goes through the browser’s JSON.parse, but only to check that it is valid. The reformatting then walks through your own text character by character: white space outside strings is removed, and line breaks and indents of two or four spaces are added after braces, brackets and commas. Empty objects and arrays are written closed up. Strings are copied untouched, escape sequences included. Minifying is the same pass without line breaks or indents.
Numbers stay as written
Reading JSON into JavaScript and writing it out again can change the numbers, because JavaScript has a single number type with limited precision. Tried with the tool’s code, the integer 12345678901234567890 becomes 12345678901234567000, the number 1.0 becomes 1, and 1e400 becomes null. In the tool all three stay exactly as written. RFC 8259 treats only integers up to 2⁵³ − 1, which is 9,007,199,254,740,991, as safe between systems, and I-JSON (RFC 7493) recommends sending exact numbers larger than that as strings.
Duplicate names
An object with the same name twice passes the check. RFC 8259 says only that names should be unique, and that what a receiver does with duplicates is unpredictable; many parsers keep only the last value. The tool keeps both, so the duplicate shows in the result. A round trip through JavaScript would have silently dropped the first value. I-JSON forbids duplicates outright.
Common errors
A tab or a line break inside a string makes the text invalid, because RFC 8259 requires the control characters U+0000–U+001F to be escaped, such as \t for a tab. It happens easily when text is pasted from a spreadsheet. NaN and Infinity, which exist in JavaScript but not in JSON, are rejected too, and so is a byte order mark at the start, although RFC 8259 allows parsers to ignore one.