Files and data
Handle a leading UTF-8 BOM in text imports
If your importer permits a leading byte-order mark, remove only the first decoded U+FEFF before JSON.parse. Do not strip that character indiscriminately from the whole document.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const text = "\uFEFF{\"ok\":true}";
const withoutBom = text.startsWith("\uFEFF") ? text.slice(1) : text;
console.log(JSON.parse(withoutBom).ok);How it works
A UTF-8 BOM is an optional leading byte sequence that may appear in files from some editors or export systems. After decoding, it can appear as a leading character. Handling that specific position keeps the rest of the data unchanged.
Limits and mistakes
A BOM is not proof that the remaining bytes use the expected encoding. Different decoders may already remove it, so keep the operation conditional. Do not use this small cleanup to hide arbitrary garbage before the JSON value.
Check the result
Expect true. Test files with and without a leading BOM and include a string value containing U+FEFF internally to confirm that the content is preserved.
Sources
Reference documentation for the behavior used in this guide: