kubernetes/kops · error
unable to marshal JSON: %v
Error message
unable to marshal JSON: %v
What it means
For `kops get instances --output json`, the renderable instance list is serialized with encoding/json.Marshal; a failure is wrapped as "unable to marshal JSON: %v". json.Marshal fails only on values it cannot encode (unsupported types like channels/funcs, or NaN/Inf floats), which is unusual here since renderableCloudInstance contains only plain string/slice fields.
Source
Thrown at cmd/kops/get_instances.go:171
cg.AdjustNeedUpdate()
}
switch options.Output {
case OutputTable:
return instanceOutputTable(cloudInstances, out)
case OutputYaml:
y, err := yaml.Marshal(asRenderable(cloudInstances))
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)
}
return nil
case OutputJSON:
j, err := json.Marshal(asRenderable(cloudInstances))
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)
}
return nil
default:
return fmt.Errorf("unsupported output format: %q", options.Output)
}
}
func instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {
fmt.Println("")
t := &tables.Table{}
t.AddColumn("ID", func(i *cloudinstances.CloudInstance) string {
return i.ID
})
t.AddColumn("NODE-NAME", func(i *cloudinstances.CloudInstance) string {
node := i.NodeView on GitHub (pinned to 4c8573c808)
Solutions
- Read the wrapped message — it names the offending type/value (e.g. 'json: unsupported value: NaN') and the failing field
- Use `kops get instances` (table) to find the instance producing the bad value and inspect it in your cloud console
- Sanitize upstream metadata (instance tags/machine types) that injects NaN, Inf, or invalid UTF-8
- Upgrade to a stock kops release to rule out a local fork/patch adding non-encodable fields
Example fix
// before MachineType: ci.MachineType, // may carry invalid data from provider // after: sanitize strings before marshaling MachineType: strings.ToValidUTF8(ci.MachineType, "\uFFFD"),
Defensive patterns
Strategy: try-catch
Type guard
func jsonSafe(instances []*cloudinstances.CloudInstance) bool {
_, err := json.Marshal(asRenderable(instances))
return err == nil
} Try / catch
j, err := json.Marshal(asRenderable(cloudInstances))
if err != nil {
return fmt.Errorf("unable to marshal JSON: %w", err)
} Prevention
- Keep renderable structs limited to string/int fields the stdlib can always encode
- Sanitize any numeric data derived from provider metrics (reject NaN/Inf)
- Run `kops get instances -o json` in CI to catch encoding regressions early
- Read the wrapped error message — it names the exact unsupported value and field
When it happens
Trigger: `kops get instances -o json` where json.Marshal(asRenderable(cloudInstances)) at get_instances.go:169-171 returns an error — e.g. instance metadata injected NaN/Inf or invalid values into a numeric field of the renderable struct.
Common situations: Custom patches or forks that add numeric fields populated with NaN from cloud provider metrics; invalid UTF-8 strings from instance tags; broken vendored cloudinstances types.
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 building annotation patch: %v
- building node patch: %w
- error building node patch: %v
- marshalling nodeupConfig: %w
- error marshaling json: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/c0b9024d8bc866fe.
Report an issue: GitHub.