What counts as valid JSON
JSON is defined by RFC 8259 (and the equivalent ECMA-404). It is deliberately small: objects in curly braces, arrays in square brackets, strings, numbers, true, false and null. This formatter validates by running your input through the browser's own JSON.parse, so anything it accepts is strictly spec-compliant JSON, and the error messages you see are the browser's.
The rules most often broken by hand-written JSON are these.
- Strings and keys must use double quotes. Single quotes are not valid.
- No trailing commas after the last item in an object or array.
- No comments, either
//or/* */. - No
undefined,NaN,Infinity, or unquoted keys; those are JavaScript, not JSON. - Numbers cannot have leading zeros (
007) or a leading plus sign.
Worked example
Beautifying {"name":"Ada","tags":["a","b"],"n":1.50} with 2-space indentation gives:
{
"name": "Ada",
"tags": [
"a",
"b"
],
"n": 1.5
}
Each nesting level adds two spaces, so the array items sit four spaces in.
Notice that 1.50 became 1.5. The tool parses and then re-serialises your data, which is what guarantees clean output, but it also means the text is rebuilt rather than reformatted in place. For the invalid input {"a":1,}, Chrome and Node report "Expected double-quoted property name in JSON at position 7 (line 1 column 8)". Position 7 is zero-based, pointing at the closing brace after the stray comma. Other browsers word it differently.
Beautify, minify and indent
Beautify offers 2 spaces, 4 spaces or tabs. Minify removes all optional whitespace to give the smallest valid text, which is useful for config values embedded in code, query parameters and request bodies. Both operations validate first; if parsing fails, your text is left untouched and the error is shown.
Things that change when you reformat
- Large integers lose precision. JavaScript numbers are 64-bit floats, so integers above 9007199254740991 are rounded.
12345678901234567890comes back as12345678901234567000. IDs that large, such as Twitter-style snowflakes, should be strings in the source data. - Duplicate keys collapse. If an object repeats a key, only the last value survives.
- Key order can change. JavaScript puts integer-like keys such as
"1"first in ascending order, so{"b":1,"1":2}comes back with"1"first. - Number formatting is normalised.
1.50becomes1.5and1E3becomes1000.
If your goal is a different format, use the JSON to YAML converter or JSON to CSV. For a longer walk-through of reading and debugging JSON, see the JSON formatting guide.