python data_error ai_generated true

pydantic.errors.JsonError: Invalid JSON

ID: python/pydantic-json-load-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-05-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

When using Json type, the input string is not valid JSON, causing parsing failure.

generic

中文

使用 Json 类型时,输入字符串不是有效的 JSON,导致解析失败。

Workarounds

  1. 90% success
    Use Json type: data: Json
  2. 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:

  1. 60% fail

    If input is already a dict, json.loads fails; Pydantic expects string.

  2. 80% fail

    Empty string is not valid JSON.