Why JSON.parse Fails on Valid JSON (Hidden Unicode Characters)
Source: Dev.to
The Problem: JSON.parse Throws “Unexpected token”
Sometimes JSON.parse throws an “Unexpected token” error even when the JSON looks completely valid. This can be especially confusing when the JSON is copied from sources like Slack, Word, Notion, or ChatGPT.
const json = '{ "name": "John" }';
The string looks correct, but hidden Unicode characters may be present.
Common Culprits
- Zero Width Space –
U+200B - Byte Order Mark (BOM) –
U+FEFF - Non‑breaking space –
U+00A0
These characters are invisible in most editors but break strict parsers.
Example with a Hidden Character
{ "name": "John" }
The tiny invisible character before the quote can cause an error such as:
Unexpected token in JSON at position X
Detecting and Removing Hidden Characters
You can strip common hidden characters with a regular expression:
const clean = str.replace(/[\u200B-\u200D\uFEFF]/g, "");
Before stripping, it’s useful to log the character codes to confirm their presence.
A Browser Tool for Detection
While debugging this issue I built a small browser tool that helps detect and remove invisible Unicode characters. It highlights:
- Zero Width Space
- BOM
- Non‑breaking spaces
- Other hidden Unicode characters
You can try it here:
The tool runs 100 % client‑side, so your text never leaves your browser.
What to Do When JSON.parse Fails
- Check for hidden Unicode characters – inspect the string for invisible code points.
- Log character codes – iterate over the string and output
charCodeAtvalues. - Strip zero‑width characters – use the regex shown above or a similar approach.
Invisible characters are a surprisingly common source of parsing bugs. Removing them restores normal JSON.parse behavior.