python
data_error
ai_generated
true
pydantic.errors.JsonError: Invalid JSON
ID: python/pydantic-json-load-error
80%Fix Rate
82%Confidence
0Evidence
2024-05-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
When using Json type, the input string is not valid JSON, causing parsing failure.
generic中文
使用 Json 类型时,输入字符串不是有效的 JSON,导致解析失败。
Workarounds
-
90% success
Use Json type: data: Json
-
85% success
Validate with custom validator: @field_validator('data') def check_json(cls, v): json.loads(v); return v
Dead Ends
Common approaches that don't work:
-
60% fail
If input is already a dict, json.loads fails; Pydantic expects string.
-
80% fail
Empty string is not valid JSON.