{"record":{"id":"49f8d7714406aa1a","repo":"kubernetes/kops","slug":"unable-to-marshal-json-v-49f8d7","errorCode":null,"errorMessage":"unable to marshal JSON: %v","messagePattern":"unable to marshal JSON: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_keypairs.go","lineNumber":249,"sourceCode":"\t\tcolumnNames := []string{\"NAME\", \"ID\", \"ISSUED\", \"EXPIRES\"}\n\t\tif options.Distrusted {\n\t\t\tcolumnNames = append(columnNames, \"DISTRUSTED\")\n\t\t}\n\t\tcolumnNames = append(columnNames, \"PRIMARY\", \"HASPRIVATE\")\n\t\treturn t.Render(items, out, columnNames...)\n\n\tcase OutputYaml:\n\t\ty, err := yaml.Marshal(items)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal YAML: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(y); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\tcase OutputJSON:\n\t\tj, err := json.Marshal(items)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal JSON: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(j); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\n\tdefault:\n\t\treturn fmt.Errorf(\"unknown output format: %q\", options.Output)\n\t}\n\n\treturn nil\n}\n\nfunc completeGetKeypairs(ctx context.Context, f commandutils.Factory, options *GetKeypairsOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {\n\tcommandutils.ConfigureKlogForCompletion()\n\n\tcluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil)\n\tif cluster == nil {\n\t\treturn completions, directive","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_keypairs.go#L231-L267","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"// before\ntype keypairItem struct {\n\tCert *x509.Certificate // fields fine, but a chan/func field would break Marshal\n}\n// after\nEnsure all exported fields are JSON-encodable, or add:\nfunc (i *keypairItem) MarshalJSON() ([]byte, error) { ... }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := runKops(\"get\", \"keypairs\", \"-o\", \"json\"); err != nil {\n\tif strings.Contains(err.Error(), \"unable to marshal JSON\") {\n\t\t// data-shape problem: fall back to table output and report wrapped cause\n\t}\n\treturn err\n}","preventionTips":["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."],"tags":["json","serialization","cli"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}