JSON fails at position 0 because of a byte-order mark (BOM)

A byte-order mark is the invisible character U+FEFF that some Windows tools write at the start of UTF-8 files. It is not part of JSON, so strict parsers stop at position 0 even though the file looks perfect. The error message often shows an empty-looking token or, in Go, the character ï (the BOM’s first byte read as Latin-1). PasteKit removes the BOM and reports it as information, so this sample formats normally and the note tells you the mark was there.

Seen as:

  • json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
  • invalid character 'ï' looking for beginning of value
  • SyntaxError: Unexpected token '', "{ "name"... is not valid JSON
  • SyntaxError: Unexpected token  in JSON at position 0

Input

Settings

History

Load from URL

Common causes

1. Python opening the file as plain utf-8

Python’s json module refuses a leading BOM and tells you the fix in the message: open the file with the utf-8-sig codec, which strips the mark if present.

Before
with open('config.json', encoding='utf-8') as f:
    config = json.load(f)
After
with open('config.json', encoding='utf-8-sig') as f:
    config = json.load(f)

2. Node.js readFileSync keeps the BOM

fs.readFileSync(path, 'utf8') returns the BOM as the first character, and JSON.parse rejects it. Strip it before parsing.

Before
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
After
const config = JSON.parse(fs.readFileSync('config.json', 'utf8').replace(/^\uFEFF/, ''));

3. Windows PowerShell 5.1 or an editor saving "UTF-8 with BOM"

Out-File -Encoding utf8 and Set-Content -Encoding UTF8 in Windows PowerShell 5.1, Visual Studio and older Notepad versions write the BOM. Save as plain UTF-8 instead; in PowerShell 7 use -Encoding utf8NoBOM.

Before
{"name": "inventory-service"}
After
{"name": "inventory-service"}

Frequently asked questions

How can I tell whether a file has a BOM?

Look at the first three bytes in a hex viewer: EF BB BF is a UTF-8 BOM. VS Code shows “UTF-8 with BOM” in the status bar, and PasteKit reports “Removed a UTF-8 byte-order mark” when it finds one.

Is a BOM allowed in JSON?

RFC 8259 says implementations must not add a BOM to JSON text, and may ignore one when parsing. Many parsers choose not to ignore it, so files with a BOM are unreliable.

Why does Go show the letter ï?

Go’s decoder reports the first unexpected byte. The BOM starts with byte 0xEF, which is printed as ï, the Latin-1 character with that code.

Related