Sooner or later, every developer stares at SyntaxError: Unexpected token and wonders where in a two-thousand-line payload the problem hides. The good news is that JSON failures are highly repetitive: a handful of mistakes cause almost all of them. In this lesson of our JSON tutorial series, we catalog the most common errors, show the exact messages they produce, and give you a reliable debugging workflow.
{ "a": 1, "b": 2, }JavaScript object literals allow the final comma; JSON does not. Browsers report something like Unexpected token } in JSON at position 18. Fix: delete the comma after the last pair or element.
{ 'name': 'Ada' }Both keys and string values must use double quotes. This error is common when payloads are assembled by hand or copied from Python repr output, which prints dicts with single quotes. Fix: replace with double quotes, or better, never build JSON by string concatenation; always use JSON.stringify or json.dumps.
{ name: "Ada" } is a valid JavaScript literal and invalid JSON. Parsers fail at the first character of the key. This frequently appears when someone pastes JavaScript code into a .json config file.
Calling JSON.parse on a value that is already an object produces Unexpected token o in JSON at position 1, because the object is coerced to the string [object Object]. The mirror image is forgetting to parse and then wondering why data.user is undefined on a string. Fix: check typeof value before parsing, and remember that response.json() already returns parsed data; never parse it again.
JSON.stringify({ x: undefined }) silently drops the key, and JSON.stringify(NaN) produces the string "null". Neither throws, which makes these bugs quiet. If a field vanishes from your payload, look for undefined. If numbers arrive as null, look for NaN or Infinity upstream.
TypeError: Converting circular structure to JSON appears when an object graph references itself, common with DOM nodes, ORM entities, and framework state. Fix: serialize a plain projection of the data you actually need, or use a replacer that skips the cyclic property.
A UTF-8 byte order mark at the start of a file, smart quotes pasted from a word processor, or non-breaking spaces can all break parsing with baffling messages, since the offending characters are invisible. Fix: save files as UTF-8 without BOM and retype quotes in your editor.
Unexpected token < in JSON at position 0 almost always means you received HTML, typically an error page or a login redirect, and tried to parse it. Inspect the raw response body and the HTTP status before parsing.
typeof and the first 100 characters of whatever you are about to parse.JSON.parse in try/catch and include the raw snippet in your error logs.Unexpected token o means double parsing; Unexpected token < means you got HTML.undefined disappears silently and NaN becomes null; circular structures throw.Next up: JSON Best Practices and Tools, where we wrap up the series with professional conventions and a tour of the ecosystem.