go type_error ai_generated true

json: cannot unmarshal number into Go value of type string

ID: go/json-unmarshal-type-mismatch

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.14 active
1.15 active
1.16 active

Root Cause

The JSON data contains a value of a different type than the target Go field, such as a number where a string is expected.

generic

中文

JSON 数据包含与目标 Go 字段类型不同的值,例如在期望字符串的地方出现了数字。

Workarounds

  1. 80% success
    Define the struct field as interface{} and then type assert.
  2. 85% success
    Use a custom UnmarshalJSON method to handle type conversion.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    This only works if the field is of type json.Number, not string.

  2. 90% fail

    Tags do not change the type conversion.