JSONRepair

Fixing the JSON BOM Byte Order Mark Parse Error: An Invisible Character at Position 0

The JSON BOM byte order mark parse error shows up as "SyntaxError: Unexpected token in JSON at position 0" in a file that looks perfectly valid. I have lost an embarrassing amount of time to this one. The fix takes one line. Finding the cause took a hex dump.

The file opened fine. My editor showed clean JSON. I even retyped the first line by hand, and the error stayed put. That retyping habit had fixed every other invisible-character bug I had ever hit, so when it failed this time I did what I should have done from the start: I stopped reading the file and started reading the bytes.

charCodeAt(0) returned 65279. Not 123, the code for {. 65279 is U+FEFF, the byte order mark, sitting in front of my opening brace like it owned the place.

What a BOM is and why it breaks your JSON

A byte order mark is a relic. It was designed for UTF-16, where it tells the reader which byte order the file uses. When a Windows editor saves "UTF-8 with BOM," it sticks the three bytes EF BB BF at the very front of the file. Most editors then hide it. You see { "name": "Alice" }. The parser sees \uFEFF{ "name": "Alice" } and dies at position 0, because no valid JSON document starts with a zero-width no-break space.

What makes it sting is that the JSON spec, RFC 8259, says implementations must not add a byte order mark to transmitted JSON text. The producer of your file violated the spec, your editor conspired to hide the evidence, and JavaScript took the blame. The three of them make a great team.

Where the BOM comes from

Usual suspects, in the order I check them:

1. Windows Notepad, especially older versions, defaults to saving UTF-8 with BOM. Someone edited your config file in Notepad and the BOM hitchhiked in.

2. Excel CSV exports and some data tools prepend one. If your JSON was ever a spreadsheet, this is suspect number one.

3. PowerShell: Out-File -Encoding utf8 writes a BOM. The fix on that side is writing with a BOM-less encoding.

4. APIs and proxies: some gateways add a BOM to the response body. Then your fetch gives you JSON that looks clean in the network tab and fails in response.json().

There is a cousin to this bug worth a sentence. A file actually encoded in UTF-16 that you read as UTF-8 also starts with garbage bytes and fails at position 0. Same symptom, different cure. If the strip below does not fix it, check the encoding, not just the BOM.

The one-line fix: strip the BOM before you parse

const text = await response.text();
const cleaned = text.replace(/^\uFEFF/, "");
const data = JSON.parse(cleaned);

Or the explicit version, which I prefer because it says what it means:

function stripBOM(str) {
  return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}
const data = JSON.parse(stripBOM(raw));

Detection first, though. Do not carpet-strip characters you cannot name. Run raw.charCodeAt(0) === 65279 or look at a hex dump of the first bytes: EF BB BF before the opening brace is the smoking gun. In VS Code, glance at the bottom status bar: it says "UTF-8 with BOM" instead of "UTF-8". "Save with Encoding" lets you re-save without it.

The prevention conversation nobody has

Stripping the BOM at parse time fixes your code. Fixing the producer fixes the problem. If a teammate's editor writes BOMs, that teammate's editor should stop. If your API emits them, your API should stop. I say this because I have watched teams carry a stripBOM call through three codebases for years while the actual producer kept emitting bad files the whole time. Strip at parse time when you have to, but open the ticket against the producer.

One more edge I keep around for interviews: String.prototype.trim() does remove U+FEFF, so a quick raw.trim() before parsing would also kill it. I do not recommend relying on that, because the next reader will assume the trim was about whitespace and remove it. Say the name of the thing you are removing.

This is the rare parse error our repair tool handles almost too well: paste the file in, it strips the BOM and shows you valid JSON, and you never learn what bit you. I would rather you know. If the "unexpected token" is at position 0 and the first character looks right, read the bytes. 65279 is the answer more often than you would guess.

Pasted JSON that looks fine but won't parse?

Drop it in. We strip BOMs, smart quotes, and control characters, and show you what changed.

Repair My JSON Free

Frequently asked questions

What is a BOM in JSON and why does it break parsing?

A BOM (byte order mark) is the invisible Unicode character U+FEFF placed at the very start of a text file. JSON.parse expects the first character to be { or [, so it throws "Unexpected token" at position 0 when it meets the BOM instead. RFC 8259 says JSON must not be transmitted with a BOM.

Where does a BOM come from in a JSON file?

Older Windows Notepad versions and some Excel CSV exports save as "UTF-8 with BOM." PowerShell's Out-File -Encoding utf8 adds one too, and some proxies add a BOM to response bodies. The character is invisible in most editors, which is why the file looks valid.

How do I detect a BOM in my JSON?

Check raw.charCodeAt(0) === 65279, or look for EF BB BF at the start of a hex dump. In VS Code the status bar encoding indicator reads "UTF-8 with BOM" instead of "UTF-8".

How do I strip a BOM before JSON.parse?

Use text.replace(/^\uFEFF/, '') before parsing, or slice(1) when charCodeAt(0) is 0xFEFF. Long term, save files as UTF-8 without BOM and fix any producer that emits them.

Related reading: Bad Control Character in String Literal: The Invisible Newline That Breaks JSON · Unexpected Token 'u' in JSON at Position 0: You Parsed the Word "undefined" · "Unexpected End of JSON Input": Empty Body, Cut-Off Payload, or Unclosed Bracket · Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse