JSON is the format most web services use to exchange data. It looks like JavaScript, but its rules are stricter, and one small slip makes a whole document invalid. Here is what the format allows and how to fix the usual mistakes.
The rules in one minute
- The top level can be an object, an array, a string, a number,
true,falseornull. - Objects use curly braces with
"key": valuepairs separated by commas. - Arrays use square brackets with values separated by commas.
- Strings and keys must use double quotes.
- Numbers are written in decimal, with no leading plus sign, no leading zeros such as
007, and noNaNorInfinity. - There are no comments and no trailing commas.
The most common errors
Trailing comma. {"a": 1,} and [1, 2,] are invalid. Remove the last comma.
Single quotes. {'a': 1} is valid JavaScript but not valid JSON. Use double quotes.
Unquoted keys. {a: 1} needs to be {"a": 1}.
Comments. // note and /* note */ are not allowed. If you need notes, put them in a field such as "_comment", or use a format designed for config files.
Missing comma or colon. Two values side by side without a comma, or a key without a colon, break the structure.
Unescaped characters in strings. A line break inside a string must be written as \n, a tab as \t, a double quote as \" and a backslash as \\.
Unclosed brackets. Every { and [ needs a closing partner. Indenting the text makes a missing one easy to spot.
Reading an error message
A good validator reports a line and a column. Go to that position, and remember the real mistake is often just before it. For example, the error for a missing comma is reported at the start of the next item, not at the end of the previous one.
Format versus minify
Formatting adds line breaks and indentation so people can read the data. Minifying removes optional spaces and line breaks so the file is smaller to send or store. Both versions contain exactly the same data. Sorting keys alphabetically does not change the meaning either, and it makes two versions of a file easier to compare.
Two quiet problems
Duplicate keys. The standard says keys should be unique. If a key appears twice, most programs silently keep the last value. A validator that warns about duplicates helps you catch a copy-and-paste mistake.
Very large numbers. JavaScript and many other tools store numbers as 64-bit floating point values, which are exact only up to 9,007,199,254,740,991. An identifier like 12345678901234567890 can be silently rounded by those programs. If you have IDs this large, send them as strings.
Is it safe to paste data into an online tool?
Only if the page processes it locally. This formatter works in your browser and does not upload what you paste. Even so, do not paste passwords, API keys or other secrets into any web page you do not fully trust.
Try it
Paste or open a file to see whether it is valid, with the line and column of any error. Choose 2 spaces, 4 spaces or tabs, or minify it. If you need to encode data for a link or a header, the Base64 and URL Encoder can help.