Reading JSON error messages

"Unexpected token in JSON at position 42" is unhelpful only until you learn the six things JSON parsers actually complain about. This guide walks through each one, what the error really means, and the fastest fix.

First: always get the line and column

A parser tells you exactly where it choked. The problem is that error position is often one character after the mistake — the parser reports where it got confused, which can be a few characters past the actual typo. When you get an error, go to that position, then scan backwards.

Big JSON files make eyeballing useless, so let the tool do it: paste the file into the JSON formatter and it reports the exact line and column, then formats the rest once the error is fixed.

The six most common errors

1. Trailing comma

JavaScript object literals allow [1, 2, 3,]. JSON does not — the last element must not be followed by a comma. The error usually reads "Unexpected token ]" or "Unexpected end of JSON input", pointing at the bracket after the comma. Delete the comma.

2. Unquoted or single-quoted keys

{name: "x"} and {'name': "x"} are both invalid. JSON requires double quotes on every key: {"name": "x"}. This is the number one error when hand-writing JSON or pasting from JavaScript source code. The error points at the key name or the colon.

3. Single quotes on string values

{"name": 'x'} is invalid — values need double quotes too. If the text came from another language's source code, this is almost always present alongside error #2.

4. Comments

// note and /* block */ do not exist in JSON. The error is "Unexpected token /". If you need annotations, put them in a "_comment": "..." key (many config systems accept this), or strip them before parsing. What allows comments is JSON5 or JSONC — different formats, different parsers.

5. Literal control characters or smart quotes

Two sneaky ones from copy-paste:

  • Curly quotes " " from a word processor or chat message are not JSON quotes. The error points at the string start.
  • A raw line break or tab inside a string value is illegal — it must be escaped as \n or \t. Error: "Bad control character in string literal".

6. Duplicate keys

{"a": 1, "a": 2} parses successfully in most parsers — the last value silently wins. It is not an error, but it is a bug: some parsers and languages keep only the first, some the last, and lossless round-tripping is impossible. If your data matters, check for duplicates.

Encoding problems that look like syntax errors

  • BOM. A file saved as UTF-8 with BOM starts with invisible bytes EF BB BF. Strict parsers reject it with a confusing "Unexpected token" at position 0. Strip the BOM.
  • HTML instead of JSON. An API returning <!DOCTYPE html> (a login page, an error page) is not a JSON problem in your code — look at what the server actually returned, usually a redirect or a proxy error.
  • Escaped-escaped text. If your value contains literal \" where you wanted a quote, the string was double-encoded. Decode one layer first.

A five-minute debugging routine

  1. Paste the JSON into the formatter and read the exact line/column.
  2. Look at that position and one or two lines above it — trailing commas and unclosed strings often open lines earlier.
  3. Check for single quotes, comments and smart quotes — the copy-paste classics.
  4. Still failing? Minify the JSON (the tool does it in one click); minified output often makes a structural problem visible because everything fits on one screen.
  5. Fixing by hand for a huge file? Fix the first error, re-validate, repeat — parsers stop at the first error, so there may be several.