# json: unsupported type: map[int]string

- **ID:** `go/json-marshal-error-unsupported-type`
- **Domain:** go
- **Category:** encoding_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

Attempting to marshal a Go type with keys that are not strings, which JSON format does not support (JSON object keys must be strings).

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| go1.19 | active | — | — |
| go1.20 | active | — | — |
| go1.21 | active | — | — |
| go1.22 | active | — | — |

## Workarounds

1. **Convert map keys to strings before marshaling: `newMap := make(map[string]string); for k, v := range oldMap { newMap[fmt.Sprintf("%d", k)] = v }`** (95% success)
   ```
   Convert map keys to strings before marshaling: `newMap := make(map[string]string); for k, v := range oldMap { newMap[fmt.Sprintf("%d", k)] = v }`
   ```
2. **Define a custom struct or use a slice of key-value pairs instead: `type Entry struct { Key int; Value string }` then marshal `[]Entry`.** (90% success)
   ```
   Define a custom struct or use a slice of key-value pairs instead: `type Entry struct { Key int; Value string }` then marshal `[]Entry`.
   ```

## Dead Ends

- **** — MarshalIndent also calls json.Marshal internally; same error occurs. (90% fail)
- **** — Returning nil causes json.Marshal to serialize the value as 'null', not skip it; error still may occur depending on implementation. (60% fail)
