kubernetes/kops · error
unable to marshal JSON: %v
Error message
unable to marshal JSON: %v
What it means
RunGetAssets uses encoding/json.Marshal on the AssetResult for `kops get assets -o json` and wraps marshal failures with this message. AssetResult contains only pointer-to-struct slices of strings, so failures typically indicate a programming/data error rather than user misconfiguration.
Source
Thrown at cmd/kops/get_assets.go:169
switch options.Output {
case OutputTable:
if err = imageOutputTable(result.Images, out); err != nil {
return err
}
return fileOutputTable(result.Files, out)
case OutputYaml:
y, err := yaml.Marshal(result)
if err != nil {
return fmt.Errorf("unable to marshal YAML: %v", err)
}
if _, err := out.Write(y); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
case OutputJSON:
j, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("unable to marshal JSON: %v", err)
}
if _, err := out.Write(j); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
default:
return fmt.Errorf("unsupported output format: %q", options.Output)
}
return nil
}
func imageOutputTable(images []*Image, out io.Writer) error {
fmt.Println("")
t := &tables.Table{}
t.AddColumn("CANONICAL", func(i *Image) string {
return i.Canonical
})
t.AddColumn("DOWNLOAD", func(i *Image) string {View on GitHub (pinned to 4c8573c808)
Solutions
- Read the wrapped inner error to find the offending field
- Ensure all AssetResult fields are JSON-marshalable (plain data types)
- Retry with -o yaml or default table output to confirm the data itself is fine
- Rebuild from upstream kops if running a patched binary
Example fix
// before
type AssetResult struct {
Images []*Image
Extra chan string // unmarshalable
}
// after
type AssetResult struct {
Images []*Image `json:"images,omitempty"`
Extra string `json:"extra,omitempty"`
} Defensive patterns
Strategy: validation
Validate before calling
// ensure result contains only JSON-marshalable data before calling
if err := json.Valid(mustBytes(json.Marshal(result))); err != nil {
return fmt.Errorf("asset result not serializable")
} Try / catch
j, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("unable to marshal JSON: %v", err)
} Prevention
- Keep AssetResult fields limited to plain data types (string, slices)
- Avoid adding channels/funcs/cyclic structures to result structs
- Add unit tests that marshal AssetResult
- Prefer upstream kops over patched builds
When it happens
Trigger: `kops get assets -o json` where json.Marshal(result) errors — practically only possible if the result structure contains unsupported types (channels, funcs, cycles) as in modified builds.
Common situations: Custom/patched kops builds with new AssetResult fields of unmarshalable types; nil map/interface oddities introduced by plugins or forks.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- error marshaling json: %v
- error writing json to stdout: %v
- unable to marshal YAML: %v
- unable to marshal JSON: %v
- unable to marshal JSON: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/9be18e30d5e88021.
Report an issue: GitHub.