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
95%Fix Rate
88%Confidence
1Evidence
2023-09-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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.htmlWorkarounds
-
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)` -
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)`
中文步骤
Open file with utf-8-sig encoding before parsing: `with open('data.json', encoding='utf-8-sig') as f: data = json.load(f)`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:
-
50% fail
Python's json.load does not accept encoding parameter; you must use open() with encoding='utf-8-sig' before json.load.
-
40% fail
This modifies the source file and may be forgotten in CI/CD pipelines; also requires root access in some environments.
-
90% fail
This breaks all key lookups in downstream code and makes the code non-portable across different editors.