data encoding_error ai_generated true

JSON parser silently reads BOM character as part of first key name

ID: data/json-utf8-bom-silent-read

Also available as: JSON · Markdown · 中文
95%Fix Rate
88%Confidence
1Evidence
2023-09-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Python 3.11 active
Python 3.12 active
Node.js 20 active
jq 1.7 active

Root Cause

Files saved with UTF-8 BOM (Byte Order Mark, \ufeff) by Windows editors cause JSON parsers that do not strip BOM to include it in the first key name, leading to key lookup failures and silent data corruption.

generic

中文

Windows编辑器保存的UTF-8 BOM文件(字节顺序标记\ufeff)导致未去除BOM的JSON解析器将其包含在第一个键名中,造成键查找失败和静默数据损坏。

Official Documentation

https://docs.python.org/3/library/json.html

Workarounds

  1. 95% success Open file with utf-8-sig encoding before parsing: `with open('data.json', encoding='utf-8-sig') as f: data = json.load(f)`
    Open file with utf-8-sig encoding before parsing: `with open('data.json', encoding='utf-8-sig') as f: data = json.load(f)`
  2. 93% success Strip BOM from file content before parsing: `content = open('data.json', 'rb').read().lstrip(b'\xef\xbb\xbf'); data = json.loads(content)`
    Strip BOM from file content before parsing: `content = open('data.json', 'rb').read().lstrip(b'\xef\xbb\xbf'); data = json.loads(content)`

中文步骤

  1. Open file with utf-8-sig encoding before parsing: `with open('data.json', encoding='utf-8-sig') as f: data = json.load(f)`
  2. Strip BOM from file content before parsing: `content = open('data.json', 'rb').read().lstrip(b'\xef\xbb\xbf'); data = json.loads(content)`

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Python's json.load does not accept encoding parameter; you must use open() with encoding='utf-8-sig' before json.load.

  2. 40% fail

    This modifies the source file and may be forgotten in CI/CD pipelines; also requires root access in some environments.

  3. 90% fail

    This breaks all key lookups in downstream code and makes the code non-portable across different editors.