{"record":{"id":"4fa8a9655564ee76","repo":"kubernetes/kops","slug":"unable-to-marshal-json-v-4fa8a9","errorCode":null,"errorMessage":"unable to marshal JSON: %v","messagePattern":"unable to marshal JSON: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_sshpublickeys.go","lineNumber":131,"sourceCode":"\t\t}\n\t\tt := &tables.Table{}\n\t\tt.AddColumn(\"ID\", func(i *SSHKeyItem) string {\n\t\t\treturn i.ID\n\t\t})\n\t\treturn t.Render(items, out, \"ID\")\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\tdefault:\n\t\treturn fmt.Errorf(\"unknown output format: %q\", options.Output)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":113,"sourceCodeEnd":142,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_sshpublickeys.go#L113-L142","documentation":"In JSON output mode, `kops get sshpublickeys` marshals the SSHKeyItem slice with encoding/json. JSON Marshal fails on unsupported types (func, chan, cycles); when that happens the command returns \"unable to marshal JSON: %v\". Like the YAML variant, this is not reachable through user input alone with the current struct.","triggerScenarios":"Only via code changes adding unserializable fields to SSHKeyItem (or embedding types with MarshalJSON errors) in a patched/forked build.","commonSituations":"Custom kOps forks; local patches adding e.g. a func or a self-referencing pointer to the item struct.","solutions":["Read the wrapped error: json: unsupported type: <T> names the exact field type","Change the offending field to a serializable type or add json:\"-\" to omit it","Verify with `-o yaml` to see if the issue is JSON-specific (e.g. a bad MarshalJSON implementation)"],"exampleFix":"// before\ntype SSHKeyItem struct {\n\tHook func() // unsupported by encoding/json\n}\n// after\ntype SSHKeyItem struct {\n\tHook func() `json:\"-\"` // excluded from marshaling\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func jsonSerializable(v interface{}) error {\n\t_, err := json.Marshal(v)\n\treturn err\n}\n// call before rendering: if err := jsonSerializable(items); err != nil { ... }","tryCatchPattern":"if err := runGetSSHPublicKeys(opts); err != nil {\n\tif strings.HasPrefix(err.Error(), \"unable to marshal JSON:\") {\n\t\tlog.Printf(\"items not JSON-serializable: %v\", err)\n\t\treturn errUnsupportedFormat\n\t}\n\treturn err\n}","preventionTips":["Never add func/chan fields to output structs, or tag them json:\"-\"","Unit-test json.Marshal on all item types rendered as JSON","Prefer plain data types (string, []byte, ints) in CLI output models"],"tags":["cli","json","serialization"],"backgroundTag":"json-marshal-unsupported-type","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}