{
  "id": "go/json-cannot-unmarshal-object-into-go-value-of-type-slice",
  "signature": "json: cannot unmarshal object into Go value of type []main.Foo",
  "signature_zh": "json: 无法将对象反序列化为 Go 值类型 []main.Foo",
  "regex": "json: cannot unmarshal object into Go value of type \\[\\]\\S+",
  "domain": "go",
  "category": "encoding_error",
  "subcategory": null,
  "root_cause": "JSON input is an object (e.g., {}) but the target Go type is a slice, causing a type mismatch during unmarshaling.",
  "root_cause_type": "generic",
  "root_cause_zh": "JSON 输入是一个对象（例如 {}），但目标 Go 类型是切片，导致反序列化期间类型不匹配。",
  "versions": [
    {
      "version": "Go 1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "UseNumber() only affects number decoding, not structural type mismatches like object vs slice.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the JSON is an array, wrapping in a struct will cause a different mismatch; the fix must match the actual JSON structure.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "RawMessage just delays the error; you still need to unmarshal into the correct type later.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Change the target type to a struct that matches the JSON object: type Foo struct { ... } then unmarshal into &foo.",
      "success_rate": 0.9,
      "how": "Change the target type to a struct that matches the JSON object: type Foo struct { ... } then unmarshal into &foo.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the JSON is expected to be an array, fix the JSON source to output an array instead of an object.",
      "success_rate": 0.85,
      "how": "If the JSON is expected to be an array, fix the JSON source to output an array instead of an object.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use json.RawMessage to inspect the JSON structure first, then unmarshal conditionally: var raw json.RawMessage; json.Unmarshal(data, &raw); if raw[0] == '{' { ... } else if raw[0] == '[' { ... }",
      "success_rate": 0.8,
      "how": "Use json.RawMessage to inspect the JSON structure first, then unmarshal conditionally: var raw json.RawMessage; json.Unmarshal(data, &raw); if raw[0] == '{' { ... } else if raw[0] == '[' { ... }",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Change the target type to a struct that matches the JSON object: type Foo struct { ... } then unmarshal into &foo.",
    "If the JSON is expected to be an array, fix the JSON source to output an array instead of an object.",
    "Use json.RawMessage to inspect the JSON structure first, then unmarshal conditionally: var raw json.RawMessage; json.Unmarshal(data, &raw); if raw[0] == '{' { ... } else if raw[0] == '[' { ... }"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pkg.go.dev/encoding/json#UnmarshalTypeError",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-08-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}