Skip to content
json2026-07-254 min read

SyntaxError: Unexpected token is probably the JSON parse error that frontend and backend developers encounter most. The browser throws Unexpected token , in JSON at position 42, Node.js throws Unexpected token } in JSON at position 128 — you can see the position, but often can't find the problem.

JSON's syntax is actually very strict, and the causes of parse failure basically come down to a handful. This article rounds up the 5 most common JSON parse errors, each with a wrong example and a fix.

Cause 1: Trailing Comma

This is the most frequent error. JavaScript object literals allow trailing commas, but JSON does not.

// Wrong: an extra comma after the last element
{
  "name": "DevToolkit",
  "version": "2.0",
  "features": ["formatter", "validator",],
}

The error is usually Unexpected token } in JSON or Unexpected token ] in JSON, because after the comma the parser expects another value but encounters a closing bracket.

// Correct: remove the trailing comma
{
  "name": "DevToolkit",
  "version": "2.0",
  "features": ["formatter", "validator"]
}

Common scenario: When copying an object literal from JavaScript code to use as JSON, it's easy to bring along trailing commas.

Cause 2: Using Single Quotes

The JSON standard only allows double quotes. Single quotes throw Unexpected token ' in JSON directly.

// Wrong: used single quotes
{
  'name': 'DevToolkit',
  'description': 'A developer tool'
}
// Correct: use double quotes throughout
{
  "name": "DevToolkit",
  "description": "A developer tool"
}

Common scenario: When converting from Python dict output or YAML, it's easy to retain the single-quote habit.

Cause 3: Unescaped Special Characters

Certain characters in JSON strings must be escaped, or the parser will think the string ended prematurely.

// Wrong: the string contains unescaped double quotes and a raw newline
{
  "message": "He said "hello" to me",
  "path": "C:\new\test"
}

First problem: the double quotes around "hello" aren't escaped, so the parser thinks the string ends after He said . Second problem: \n and \t get interpreted as newline and tab characters.

// Correct: escape the special characters
{
  "message": "He said \"hello\" to me",
  "path": "C:\\new\\test"
}

Characters that need escaping: "\", \\\, newline → \n, tab → \t, carriage return → \r.

Common scenario: Concatenating JSON strings without escaping variable values, or dropping a Windows path straight into JSON.

Cause 4: BOM Header (Byte Order Mark)

This is a sneaky one. A UTF-8 file may start with a BOM character (\uFEFF) — invisible to the eye, but the JSON parser throws Unexpected token in JSON at position 0.

// Simulating a JSON string with a BOM
const jsonWithBOM = "\uFEFF{\"name\": \"test\"}";

JSON.parse(jsonWithBOM);
// SyntaxError: Unexpected token  in JSON at position 0

// Fix: strip the BOM
const clean = jsonWithBOM.replace(/^\uFEFF/, "");
JSON.parse(clean); // { name: "test" }

Common scenario: UTF-8 files saved by Windows Notepad include a BOM by default. If your JSON comes from a file read (fs.readFile, a local fetch), a position-0 error is almost certainly a BOM.

// Safe way to read a JSON file in Node.js
const fs = require("fs");
const raw = fs.readFileSync("config.json", "utf-8");
const data = JSON.parse(raw.replace(/^\uFEFF/, ""));

Cause 5: Comments in JSON

The JSON standard does not support any form of comments. Both // and /* */ cause parsing to fail.

// Wrong: contains comments
{
  // This is the server address
  "host": "api.example.com",
  "port": 8080 /* default port */
}

The error is usually Unexpected token / in JSON at position X.

// Correct: remove all comments
{
  "host": "api.example.com",
  "port": 8080
}

Common scenario: Parsing a .jsonc (VS Code config format) or a config file with comments directly as JSON. If you genuinely need comments, consider JSON5 or YAML.

// If you must handle JSON with comments, strip them first (simple cases)
const stripComments = (str) =>
  str.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");

JSON.parse(stripComments(jsonWithComments));

Quick Localization and Fix

When you hit an Unexpected token error, troubleshoot in this order:

  1. Look at the position number in the error and locate the specific character.
  2. Check whether the area around that position has any of the 5 issues above.
  3. If position is 0, check for a BOM first.
  4. If the JSON was generated by code, check the escaping logic in the concatenation.

For long JSON, manual inspection is painfully slow. Paste the content into the JSON Formatter — it validates syntax in real time, locally in your browser, pinpoints the exact error location, and auto-formats valid JSON. All processing happens locally, so you don't need to worry about API data or config leaking to a third-party server.


ad