JSON Parse Error 'Unexpected token'? 5 Common Causes and How to Fix Them
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 without a JSON Formatter that highlights the exact syntax error.
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. Once your JSON parses, you can take validation further with a JSON Schema Validator.
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": "ToolVault",
"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": "ToolVault",
"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': 'ToolVault',
'description': 'A developer tool'
}
// Correct: use double quotes throughout
{
"name": "ToolVault",
"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:
- Look at the position number in the error and locate the specific character.
- Check whether the area around that position has any of the 5 issues above.
- If position is 0, check for a BOM first.
- 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.
Position math that trips people up
Error offsets count characters in the string the parser received — not "visual columns" after your editor wrapped lines, and not bytes if you mixed encodings. A UTF-8 multi-byte character still advances the JSON character index by one code unit in JavaScript strings. If the reported position looks "early," check whether a proxy prepended a BOM or a logging wrapper prepended a status line before the JSON body. When the token is literally <, stop debugging JSON syntax: you are almost certainly looking at an HTML error page (see the companion article linked below).
FAQ
What does Unexpected token in JSON mean?
The parser hit a character that is illegal at that position — often a trailing comma, a single quote, an unescaped control character, a BOM at position 0, or a comment (/ ).
How do I fix "Unexpected token is not valid JSON"?
- Jump to the reported position.
- Check the five causes above.
- If the token is
'<', the body is HTML (wrong URL / auth / proxy) — see 5 reasons your API returned HTML. - Paste into the JSON Formatter to highlight the first error instantly.
Why does JavaScript allow trailing commas but JSON.parse does not?
Object / array literals in JS are a different grammar from JSON. Always JSON.stringify when emitting JSON, or strip trailing commas before parse.
Can comments exist in JSON?
Not in standard JSON. Use JSONC/JSON5/YAML if you need comments, or strip them before JSON.parse.
Provided by ToolVault. If the offending token is '<' (HTML instead of JSON), see 5 reasons your API returned HTML. Related tools: JSON Formatter, JSON Diff, JSON Schema Validator. Related reading: JSON formatting guide. See the homepage for more developer tools.
Implementer's note
Building the JSON formatter taught us the top need is not formatting but error localization: an Unexpected token without line/column says nothing. We wrap the parser with position extraction, converting token offsets into line/column and highlighting them on the source — so the debugging path in this article completes inside the tool itself.
Related Tools
Related Articles
JSON Integer Precision Trap: When Snowflake IDs Are Silently Rewritten
JSON numbers beyond 2^53-1 are rounded by JSON.parse. Compare naive formatters with a lossless path, and cross-link JWT, Base64, and this site’s JSON tools.
JSON Troubleshooting Handbook: From Error Message to Fix (Symptom Index)
JSON errors that make no sense? This handbook organizes fixes by symptom — syntax errors, Unicode escaping, encoding mojibake, and Schema validation failures, with a diagnostic tree and a general debugging workflow.
JSON Schema Reports "required" Property Missing — How to Locate and Fix
JSON Schema validation says a required property is missing, or type mismatch? Explains required / additionalProperties / type pitfalls and how to locate the exact level with our validator.