Toolstia / Guides

JSON errors explained: 6 mistakes that break your JSON

5 min read

JSON looks like JavaScript, so it is easy to write it the JavaScript way and get an error. Standard JSON is stricter. These six mistakes cause most failures.

1. A trailing comma

A comma after the last item is allowed in many programming languages but not in JSON.

{ "a": 1, "b": 2, }   <- invalid
{ "a": 1, "b": 2 }    <- valid

2. Single quotes

Strings and keys must use double quotes.

{ 'name': 'Sam' }     <- invalid
{ "name": "Sam" }     <- valid

3. Keys without quotes

Every key must be a double-quoted string, even if it is a single word.

{ name: "Sam" }       <- invalid
{ "name": "Sam" }     <- valid

4. Comments

JSON has no comment syntax. Lines starting with // or blocks inside /* */ make the file invalid. If you need notes, keep them in a separate file or use a field such as "note".

5. Unescaped special characters in strings

A double quote inside a string must be written as \", a backslash as \\, and a line break as \n. A raw line break inside a string is an error.

{ "quote": "She said "hi"" }      <- invalid
{ "quote": "She said \"hi\"" }    <- valid

6. Missing commas or brackets

Forgetting the comma between two items, or leaving a bracket or brace unclosed, is easy to do in long files. The error message usually points to the place where the parser got confused, which can be just after the real mistake.

Values that look right but are not

  • undefined, NaN and Infinity are not valid JSON values. Use null or a string instead.
  • Numbers cannot have leading zeros such as 007, or a plus sign.
  • Only true, false and null are allowed in lower case. True or NULL will fail.

How to find the problem fast

  1. Paste the JSON into the JSON formatter.
  2. Read the message under the boxes. It names the problem and gives a line and column.
  3. Look at that spot and the line just before it, fix it, and the box turns to "Valid JSON".
  4. Choose format to make it readable, or minify to shrink it.