JSON that looks perfect but will not parse is often carrying an invisible character. Here is why, and how to find and remove it.
Why JSON is so sensitive
JSON has strict syntax. The parser expects specific characters in specific places: an opening brace, quoted keys, colons, commas. An invisible character violates that structure without showing anything on screen:
- A byte-order mark (BOM) before the opening
{means the first character is not what the parser expects, so it throws an error like "unexpected token." - A zero-width space inside a key or string value changes the bytes, so a key you compare against no longer matches.
- A non-breaking space where a regular space belongs can trip stricter parsers.
Because you cannot see any of these, the error message rarely points you to the real cause.
How to find the culprit
- Check for a BOM first. Open the file in an editor that shows encoding; "UTF-8 with BOM" is the common offender.
- Search by code point. In a regex-capable editor, search for
\uFEFF(BOM),\u200B(zero-width space), and\u00A0(non-breaking space). - Paste it into a cleaner that reports hidden characters, which tells you exactly what is present without any setup.
How to fix it
Save as UTF-8 without BOM. In VS Code, click the encoding in the status bar and choose "Save with Encoding" > "UTF-8" (not "with BOM"). Most editors have the same option.
Strip the characters directly. If you are working with copied JSON, paste it into a cleaner that removes invisible characters. textscrubr strips the BOM, zero-width spaces, and other invisibles while leaving your JSON structure and spacing intact, so the parser gets clean input.
Prevent it in a pipeline
If JSON is generated or passed between tools, add a strip-BOM and strip-invisibles step so no hidden character ever reaches the parser. It turns a baffling, hard-to-reproduce bug into a solved problem.