Hidden characters in code and JSON: fix the failing location

An invisible character in a program file should not trigger “delete everything invisible.” U+200B between a colon and number makes JSON invalid; the same U+200B inside a quoted value is valid data; U+FEFF at the start of a stream may be a byte-order mark. Reproduce the error with a strict parser and locate the exact boundary first.
Three different cases
{"count":1} violates JSON grammar because U+200B is not JSON whitespace. {"name":"AB"} is syntactically valid and the character remains in the parsed string. A stream beginning with U+FEFF may be accepted by one loader and rejected by another. Those situations need separate, narrowly defined fixes.
Begin with bytes and decoding
Record the input encoding and decoder. Misreading UTF-8 as Windows-1252 produces visible mojibake that deleting Unicode controls cannot repair. For an external file, keep a byte-for-byte copy or hash, inspect the opening bytes and decode exactly once. Analyse code points only after the byte-to-text step is understood.
Repair the boundary, not the document
If the parser reports line and column, inspect a small escaped window around it. Remove a BOM only at the beginning of the stream. Remove a confirmed U+200B only where grammar forbids it. Do not globally rewrite quoted values: they may contain names, Arabic text, emoji or data where joiners are significant.
Controls before and after
- Keep the original bytes.
- Run the standard parser and save the error.
- Show an escaped fragment and code points.
- Apply the smallest transformation to a copy.
- Parse again, then validate schema, types and record counts.
A successful JSON.parse proves syntax only. It does not validate an email address, amount, identifier or business rule. Validate the resulting object separately.
Source code and review
Enable visible controls in the editor and inspect diffs. For bidirectional text, use compiler or editor warnings as well as code review. String literals can intentionally contain Unicode, so a change must be visible to the reviewer. Generated files should be fixed at the exporter or source template so the issue does not return.
JavaScript example
// Run each example separately.
JSON.parse('{"count":\u200B1}'); // SyntaxError
JSON.parse('{"name":"A\u200BB"}'); // valid; preserves U+200B
const raw = '\uFEFF{"count":1}';
JSON.parse(raw.replace(/^\uFEFF/, "")); // {count: 1}The BOM rule is anchored to the start. That is safer than replaceAll, which could alter legitimate string data. Other formats have other grammars; do not apply JSON assumptions to CSV, XML or source code.
Keep a regression fixture
Save the smallest failing file after the repair. One test should reject U+200B between JSON tokens, another should preserve it inside a quoted string, and a third should handle a BOM only at the beginning. This set prevents a later “cleanup” refactor from broadening the replacement and silently changing valid application data.
Sources and specifications
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format
- Unicode FAQ: UTF and BOM
- MDN: JSON.parse()