Common JSON Errors and How to Fix Them

beginner
10 min

Common JSON Errors and How to Fix Them

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.

Error 1: Trailing Commas

json
{ "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.

Error 2: Single Quotes

text
{ '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.

Error 3: Unquoted Keys

{ 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.

Error 4: Parsing Twice or Not at All

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.

Error 5: Undefined, NaN, and Infinity

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.

Error 6: Circular References

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.

Error 7: Encoding and Invisible Characters

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.

Error 8: The Response Was Never JSON

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.

A Debugging Workflow

  1. Read the position number in the error and jump to that offset.
  2. Pretty-print the raw text in a validator such as jsonlint.com to get a line and column.
  3. Log typeof and the first 100 characters of whatever you are about to parse.
  4. Wrap JSON.parse in try/catch and include the raw snippet in your error logs.

Key Takeaways

  • Trailing commas, single quotes, and unquoted keys cause most syntax failures.
  • Unexpected token o means double parsing; Unexpected token < means you got HTML.
  • undefined disappears silently and NaN becomes null; circular structures throw.
  • Always parse defensively: check types, catch errors, and log the raw input.

Next up: JSON Best Practices and Tools, where we wrap up the series with professional conventions and a tour of the ecosystem.