kubernetes/kops · error
unable to marshal JSON: %v
Error message
unable to marshal JSON: %v
What it means
In RunGetKeypairs' OutputJSON branch, json.Marshal(items) serializes the []*keypairItem slice. If marshaling fails, the command returns "unable to marshal JSON: %v". json.Marshal only errors on unsupported types (channels, funcs, cyclic structures), so this indicates a data/shape problem rather than user input.
Source
Thrown at cmd/kops/get_keypairs.go:249
columnNames := []string{"NAME", "ID", "ISSUED", "EXPIRES"}
if options.Distrusted {
columnNames = append(columnNames, "DISTRUSTED")
}
columnNames = append(columnNames, "PRIMARY", "HASPRIVATE")
return t.Render(items, out, columnNames...)
case OutputYaml:
y, err := yaml.Marshal(items)
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(items)
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("unknown output format: %q", options.Output)
}
return nil
}
func completeGetKeypairs(ctx context.Context, f commandutils.Factory, options *GetKeypairsOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
commandutils.ConfigureKlogForCompletion()
cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil)
if cluster == nil {
return completions, directiveView on GitHub (pinned to 4c8573c808)
Solutions
- Read the wrapped %v cause — json.Marshal errors name the exact type it cannot encode.
- Try `-o yaml` or `-o table` to still inspect the keypairs.
- Check whether local modifications to cmd/kops/get_keypairs.go introduced an unsupported field; revert or add a marshaler.
- If caused by store data, verify state-store contents and kOps version compatibility.
Example fix
// before
type keypairItem struct {
Cert *x509.Certificate // fields fine, but a chan/func field would break Marshal
}
// after
Ensure all exported fields are JSON-encodable, or add:
func (i *keypairItem) MarshalJSON() ([]byte, error) { ... } Defensive patterns
Strategy: try-catch
Try / catch
if err := runKops("get", "keypairs", "-o", "json"); err != nil {
if strings.Contains(err.Error(), "unable to marshal JSON") {
// data-shape problem: fall back to table output and report wrapped cause
}
return err
} Prevention
- Read the wrapped cause — json errors name the offending type.
- Avoid local modifications that add non-JSON-encodable fields to keypairItem.
- Fall back to -o table or -o yaml when JSON serialization fails.
- Keep the CLI and state-store data formats version-compatible.
When it happens
Trigger: `kops get keypairs -o json` where keypairItem contains a value json.Marshal cannot encode — e.g. a field of an unsupported type or an unexpected data shape loaded from the key store.
Common situations: Custom patches to keypairItem introducing unencodable fields; corrupted or unusual keyset metadata from the state store; version mismatch between the CLI and state-store data format.
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 JSON: %v
- unable to marshal JSON: %v
- error marshaling json: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/49f8d7714406aa1a.
Report an issue: GitHub.