The error SyntaxError: Unexpected token is one of the most common issues developers encounter when parsing JSON. While it often appears to be a syntax problem, the real cause is frequently invisible Unicode characters.
According to the JSON specification, only these whitespace characters are allowed:
Anything else causes an immediate parsing failure.
{
"user": "admin"
}
Looks valid. But if a hidden character exists before the first brace, parsing fails instantly.
function sanitizeJSON(input) {
return input
.replace(/[\u200B-\u200D\uFEFF]/g, "")
.replace(/\u00A0/g, " ");
}
JSON.parse(sanitizeJSON(rawInput));
Instead of manually debugging invisible characters, use:
The “Unexpected token” error is rarely mysterious. In modern workflows involving AI tools and rich text editors, invisible Unicode characters are often the hidden culprit. Cleaning the input before parsing ensures stability and predictable behavior.