Skip to content
FrAIFlow

Solutions

Why valid-looking JSON fails at the first character

You can see a valid JSON object yet still get an error at position zero. The opening brace may not be the first character: an invisible U+FEFF can sit before it. Keep the original before trying a repair. An HTML login page, empty response or incorrectly decoded file needs a different fix.

A 2017 Stack Overflow report traced a similar Node.js failure to decimal 65279, or U+FEFF. A saved local browser test is separate evidence: a synthetic U+FEFF followed by {"ok":true} failed, then parsed after only the prefix was removed. The screenshot records that local test, not the reporter’s file.

A safe repair you can actually run

Start with a copy of the file, not your only export. If this came from a web request, check its HTTP status and content type first: a login page beginning with <!doctype html> needs an authentication fix, not a JSON cleanup. For a local file, use an editor that can show its encoding. Do not save over the original while investigating.

Once you have decoded the file into a JavaScript string called text, use the following small helper in your own import script. It accepts one known leading marker and leaves everything else alone.

function parseJsonText(text) {
  if (typeof text !== "string") throw new TypeError("Expected text");
  const hadBOM = text.charCodeAt(0) === 0xFEFF;
  const cleaned = hadBOM ? text.slice(1) : text;
  return { value: JSON.parse(cleaned), removedLeadingBOM: hadBOM };
}
const sample = '\uFEFF{"ok":true,"name":"한글"}';
const result = parseJsonText(sample);
console.log(result.removedLeadingBOM); // true
console.log(result.value.ok);          // true
console.log(result.value.name);        // 한글

The success check is more than “the error disappeared.” Confirm that the expected properties exist and that non-English text still reads correctly. Run the helper on ordinary JSON too: it should parse without reporting a removed marker. An empty input or a malformed object should still throw. Keep that error visible to the caller rather than returning an empty object, which could be mistaken for valid data.

If the first code point is not U+FEFF, stop this repair path. For example, a file decoded with the wrong encoding may contain replacement characters or alternating nulls. Reopen it using the encoding selected by the exporter, or request a fresh UTF-8 export. Removing characters cannot reconstruct bytes that were decoded incorrectly.

The difference between bytes and a hidden character

Hidden prefix / U+FEFF  {"ok":true} / JSON.parse → rejected
Author-created explanation, not an application capture.

TextDecoder("utf-8") normally omits a leading UTF-8 BOM. Its ignoreBOM option is counterintuitive: true includes the marker in the decoded string. Our byte fixture EF BB BF 7B 7D became {} with the default behavior. This is not automatic encoding detection.

Check the producer

A separate PowerShell report involved the wrong decoding path. Windows PowerShell Out-File and redirection can produce UTF-16LE; newer PowerShell behavior differs. Removing non-ASCII text can destroy valid names without fixing that mismatch. Check the producing command and version, then decode or re-export correctly. That user’s workflow was not reproduced in our test.

RFC 8259 requires UTF-8 for JSON exchanged outside a closed ecosystem and prohibits producers from adding a network BOM. Fix the exporter if possible. At an import boundary, test normal JSON, the confirmed prefix, malformed JSON and non-English values. Log only the diagnostic prefix, not private document contents.

A UTF-8 BOM becomes a leading U+FEFF, causing a parse error; removing only that prefix allows the JSON to parse.
Supporting explanation, not an application screenshot.
Recorded Chrome output shows U+FEFF before the opening brace and successful parsing after that leading character is removed.
Saved local test output. Open the image to inspect the original evidence.
Sources, examples and further reading