go type_error ai_generated true

error: json: cannot unmarshal object into Go value of type string

ID: go/encoding-json-unmarshal-into-interface-with-concrete-type

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2025-11-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Attempting to unmarshal a JSON object into a Go string variable, which requires a struct or map.

generic

中文

尝试将JSON对象解组到Go字符串变量,这需要结构体或映射。

Workarounds

  1. 90% success Unmarshal into a map or struct first
    var result map[string]interface{}
    if err := json.Unmarshal(data, &result); err != nil { return err }
    strVal, ok := result["key"].(string)

Dead Ends

Common approaches that don't work:

  1. Using type assertion after unmarshal into interface 70% fail

    Still fails if target type is string; use proper struct.