Common JSON Errors and How to Fix Each One

By Undercover Nerd Project · Published November 12, 2026 · 4 min read

The ten mistakes behind almost every "Unexpected token" message, with a wrong and a right example for each, and how to find the exact spot quickly.

"Unexpected token" is the least helpful error message in programming. It tells you that something is wrong, not what or where, and the real mistake is often a few characters before the spot the parser points at. This guide covers the ten mistakes behind almost every invalid JSON document, shows a wrong and a right version of each, and ends with a method for finding the problem quickly in a large file.

1. A trailing comma

JavaScript and many other languages allow a comma after the last item. JSON does not.

{"a": 1, "b": 2,}     wrong
{"a": 1, "b": 2}      right

2. Single quotes

Strings and property names must use double quotes. Single quotes are valid in JavaScript but not in JSON.

{'name': 'Ana'}       wrong
{"name": "Ana"}       right

3. Unquoted property names

This is how you write an object in JavaScript source, and it is the most common mistake when people paste code instead of data.

{name: "Ana"}         wrong
{"name": "Ana"}       right

4. A missing comma

This usually happens after editing by hand, when a new line is added without a comma on the line above. The parser reports the problem at the start of the second item, which is one line after the actual mistake.

{"a": 1
 "b": 2}              wrong
{"a": 1,
 "b": 2}              right

5. Comments

JSON has no comment syntax, so // note and /* note */ are errors. Configuration formats that allow comments, such as JSON5 or JSONC, are different formats. If you need a note in real JSON, a common workaround is an extra property such as "_comment": "...".

6. Unescaped characters inside strings

A string may not contain a raw double quote, a raw backslash or a raw line break. Each must be escaped.

{"path": "C:\Users\Ana"}        wrong
{"path": "C:\\Users\\Ana"}      right

{"quote": "She said "hi""}      wrong
{"quote": "She said \"hi\""}    right

Windows paths and text copied from documents are the usual culprits. The valid escapes are \", \\, \/, \b, \f, \n, \r, \t and \uXXXX. Anything else after a backslash, such as \x or \', is an error.

7. Numbers JSON does not accept

JSON numbers are strict: no leading zeros, no leading plus, no hexadecimal, and a digit is required on both sides of a decimal point.

{"n": 007}   {"n": +5}   {"n": .5}   {"n": 5.}   {"n": 0xFF}   wrong
{"n": 7}     {"n": 5}    {"n": 0.5}  {"n": 5.0}  {"n": 255}    right

NaN and Infinity are not valid either. If your data can contain them, store them as null or as strings and decide how to read them back.

8. Values from other languages

Python writes True, False and None. JavaScript has undefined. JSON only has true, false and null, in lower case. This usually appears when someone prints a Python dictionary instead of serialising it with json.dumps, or when a JavaScript object with an undefined value is edited by hand.

9. Brackets that do not match

Every { needs a } and every [ needs a ], in the right order. A missing closer at the end gives "Unexpected end of JSON input", which is the parser telling you it ran out of text while still waiting for something. The mistake is somewhere above, where an opener was never closed. Editors that highlight matching brackets or indent the structure make this much easier.

10. Invisible and look-alike characters

Text copied from web pages, chat apps and word processors often contains curly quotes instead of straight ones, non-breaking spaces that look like spaces, or a byte order mark at the start of a file. They are invisible or nearly so, but the parser sees them as unexpected characters. If a document looks perfect and still fails, suspect these.

Reading the error message

Different tools phrase errors differently, but they report the same facts: what the parser expected and where it gave up. A message such as "Expected double-quoted property name at line 3 column 5" points to a trailing comma on the line above or an unquoted key. "Unexpected token" followed by a character shows what the parser found where it wanted something else. In every case, look at the line before the reported position as well, because a missing comma or an unclosed string is only detected when the next item starts.

Finding the error in a large file

  1. Format the document first. If a formatter cannot parse it, the first error reported is the first real problem, with a line and column.
  2. Fix one error at a time. Later errors are often caused by the first, so re-check after each fix.
  3. Narrow it down. If you cannot see the problem, delete half of the document and re-validate. The half that fails contains the error.
  4. Compare with a known good sample. If the data comes from an API, request one record and compare its structure with yours.

Repair carefully

Tools that automatically repair JSON are convenient, but every repair is a guess about what the author meant. Removing a trailing comma or replacing single quotes is safe. Guessing what an unclosed string should have contained is not. The Fix common errors option only applies the safe repairs and still reports anything it cannot fix with confidence. Always look at the result before you rely on it, particularly if the data matters. For the rules behind all of this, see the JSON syntax cheat sheet.

More guides