How to Fix Common JSON Errors

One misplaced character and a JSON parser throws out the entire document, usually with something like Unexpected token } in JSON at position 47. The strictness is deliberate, and it has a useful side effect: the same short list of mistakes causes almost every failure you will ever see. Here they are, broken example first and fix second, roughly in order of how often they turn up.

Reading the error message first

Unexpected token X at position N means the parser was happy until character N and then met something the grammar forbids. Look at that position, then look just above it. A comma missing two lines up is what makes the parser choke on the next token, so the reported spot and the actual mistake are often a few lines apart. Paste the document into the JSON Validator and it turns the position into a line and column, which beats counting characters.

1. Trailing commas

{ "name": "Ada", "age": 36, }   ✕
{ "name": "Ada", "age": 36 }    ✓

JavaScript tolerates a comma after the last item. JSON never has. This is the most common JSON error by a wide margin, and hand-edited config files are where it breeds. Delete the comma after the final member of every object and array.

2. Single quotes

{ 'name': 'Ada' }     ✕
{ "name": "Ada" }     ✓

Keys and values are both strings, and strings take double quotes. Single quotes usually arrive by copy-paste from Python or JavaScript source.

3. Unquoted keys

{ name: "Ada" }       ✕
{ "name": "Ada" }     ✓

Object keys are strings too, so even a plain one needs its quotes. JavaScript object literals allow bare keys, which is why a snippet pasted straight out of code fails here.

4. Comments

{
  // user record        ✕
  "name": "Ada"
}

JSON has no comment syntax. Some tools accept a superset called JSONC (VS Code’s settings file, for instance), but a standard parser rejects the document. If a config format genuinely needs commentary, strip the comments before parsing or park the text in an ignored key like "_comment".

5. Unescaped characters inside strings

{ "quote": "She said "hi"" }        ✕
{ "quote": "She said \"hi\"" }      ✓

Double quotes, backslashes and real line breaks inside a string have to be escaped as \", \\ and \n. It bites hardest when the value is code, a Windows file path full of backslashes, or several lines of text. The JSON Escape tool applies the escaping mechanically, which is more reliable than doing it by eye.

6. Wrong values: undefined, NaN, Infinity

{ "score": NaN, "next": undefined }   ✕
{ "score": null, "next": null }        ✓

Those are JavaScript, not JSON. The only empty value JSON knows is null, and the only numbers it accepts are finite ones.

7. Smart quotes and invisible characters

Text that has passed through Word, Google Docs or a chat app tends to arrive carrying typographic quotes (“ ”) in place of straight ones, or an invisible byte-order mark sitting at the front of the file. Neither shows up on screen and both are fatal to the parser. Retype the quotes in a code editor, and save files as UTF-8 without BOM.

A debugging routine that works

Validate, read the line number, fix the first error, repeat. Parsers stop at the first problem, so a document with three mistakes takes three rounds. That is normal rather than a sign you are going backwards.

Once it parses, run the result through the JSON Formatter. Indented structure makes the remaining logical problems visible: a value sitting in the wrong branch, a key spelled two different ways. And if the rules themselves are still new, What Is JSON? covers the six value types. Most syntax errors stop happening once those are second nature.

Sources and further reading