go type_error ai_generated true

json: cannot unmarshal number into Go struct field Age of type string

ID: go/encoding-json-type-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-03-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active

Root Cause

The JSON data contains a numeric value where the Go struct expects a string, causing a type mismatch during unmarshaling.

generic

中文

JSON 数据包含数字值,而 Go 结构体期望字符串,导致反序列化时类型不匹配。

Workarounds

  1. 85% success
    type Person struct { Age string `json:"age"` }; var p Person; if err := json.Unmarshal(data, &p); err != nil { ... }; // or use custom UnmarshalJSON

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Loses type safety and requires manual type assertions, increasing complexity.

  2. 90% fail

    Silently drops data, leading to incorrect application behavior.