Back to articles
Why JSON.parse Fails on Valid JSON (Hidden Unicode Characters)

Why JSON.parse Fails on Valid JSON (Hidden Unicode Characters)

via Dev.to WebdevPepsi

Sometimes JSON.parse throws an “Unexpected token” error even when the JSON looks completely valid. This confused me for a long time. Example: const json = '{ "name": "John" }'; JSON.parse(json); Looks correct, right? But sometimes when copying JSON from Slack, Word, Notion, or ChatGPT, hidden Unicode characters are inserted into the string. Common culprits include: • Zero Width Space (U+200B) • Byte Order Mark / BOM (U+FEFF) • Non-breaking space (U+00A0) These characters are invisible, but they break parsers. Example with hidden character: {​ "name": "John" } That tiny invisible character before the quote can cause: Unexpected token in JSON at position X Debugging this is frustrating because the character cannot be seen in most editors. Quick Fix in JavaScript You can remove common hidden characters with: str.replace(/[\u200B-\u200D\uFEFF]/g, "") But first you need to detect them . Tool to Detect Hidden Unicode Characters While debugging this issue I built a small browser tool that hel

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
1 views

Related Articles