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.
with open('config.json', encoding='utf-8') as f:
config = json.load(f)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.
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));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.
{"name": "inventory-service"}{"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.