JSON Parse Error on Line 4? Here's How to Actually Debug It

By the end of this walkthrough, you'll be able to take any SyntaxError: Unexpected token or Unexpected end of JSON input message and go straight to the offending character — no trial-and-error deletion of lines until the error disappears.

Prerequisites

  • Node.js 18+ installed (for reproducing errors in a terminal; browser DevTools also works)
  • The malformed JSON string or file that's failing
  • A text editor with line numbers visible
  • Access to a browser-based JSON validator for the visual cross-check step

Step 1: Read the Full Error Message, Not Just "Line 4"

JSON.parse() errors in V8 (Node.js and Chrome) report a character position, not always a line number directly. A typical message looks like:

SyntaxError: Unexpected token } in JSON at position 87

Firefox's SpiderMonkey engine reports it differently, often including line and column directly:

SyntaxError: JSON.parse: unexpected character at line 4 column 3 of the JSON data

The engine you're running in changes what the error hands you. If you're debugging in Node, you'll usually get a raw position (a character offset from the start of the string), which is why "line 4" from a stack trace or linter is sometimes an approximation, not the engine's own output.