{"record":{"id":"a5244ef7bdac639e","repo":"kubernetes/kops","slug":"error-marshaling-json-v","errorCode":null,"errorMessage":"error marshaling json: %v","messagePattern":"error marshaling json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get.go","lineNumber":117,"sourceCode":"\t\treturn fmt.Errorf(\"error writing to stdout: %v\", err)\n\t}\n\treturn nil\n}\n\n// obj must be a pointer to a marshalable object\nfunc marshalYaml(obj runtime.Object) ([]byte, error) {\n\ty, err := kopscodecs.ToVersionedYaml(obj)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error marshaling yaml: %v\", err)\n\t}\n\treturn y, nil\n}\n\n// obj must be a pointer to a marshalable object\nfunc marshalJSON(obj runtime.Object) ([]byte, error) {\n\tj, err := kopscodecs.ToVersionedJSON(obj)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error marshaling json: %v\", err)\n\t}\n\treturn j, nil\n}\n","sourceCodeStart":99,"sourceCodeEnd":121,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get.go#L99-L121","documentation":"marshalJSON wraps errors from kopscodecs.ToVersionedJSON, which converts a kops runtime.Object to versioned JSON via the apimachinery codec machinery. It fires when the object cannot be converted to the target versioned GVK or serialized, e.g. because the object was not registered in the scheme or contains unserializable data. The wrapper preserves the underlying codec error for diagnosis.","triggerScenarios":"Calling marshalJSON with a runtime.Object whose type is not registered in the kops scheme, an unregistered/nil GVK after conversion, or an object containing fields that fail JSON serialization (e.g. invalid nested values).","commonSituations":"Developer adds a new kops API kind but forgets to add it to the scheme; passing an unstructured object where a registered typed object is expected; kops version skew where an object kind was removed or renamed; piping `kops get ... -o json` for an object the codecs cannot version.","solutions":["Inspect the wrapped underlying error (%v) to see whether the kind is unregistered or serialization failed","Register the object's type (AddToScheme / SchemeBuilder registration) if it is a new or custom kind","Ensure the object passed is non-nil and its GroupVersionKind is set or derivable via the scheme","Upgrade/downgrade kops so CLI and state-store cluster versions agree on API versions"],"exampleFix":"// before\nj, err := kopscodecs.ToVersionedJSON(someUnregisteredObject)\n// after\nscheme := runtime.NewScheme()\napi.AddToScheme(scheme) // ensure kind is registered\nj, err := kopscodecs.ToVersionedJSON(obj)","handlingStrategy":"validation","validationCode":"// before calling kopscodecs.ToVersionedJSON\nif obj == nil || reflect.ValueOf(obj).Kind() != reflect.Ptr {\n    return fmt.Errorf(\"marshalJSON: obj must be a non-nil pointer\")\n}\nkinds, _, err := scheme.ObjectKinds(obj)\nif err != nil || len(kinds) == 0 {\n    return fmt.Errorf(\"object type %T not registered in scheme: %w\", obj, err)\n}","typeGuard":"func isMarshalable(obj runtime.Object) bool {\n    if obj == nil { return false }\n    _, _, err := scheme.ObjectKinds(obj)\n    return err == nil\n}","tryCatchPattern":"j, err := marshalJSON(obj)\nif err != nil {\n    var unreg *runtime.NotRegisteredErr\n    if errors.As(err, &unreg) {\n        // register kind in scheme and retry\n    }\n    return fmt.Errorf(\"error marshaling json: %v\", err)\n}","preventionTips":["Always call api.AddToScheme on any custom scheme before serializing new kinds","Pass non-nil pointers to marshalable objects","Keep kops CLI and state-store API versions aligned","Test serialization of new API kinds in unit tests"],"tags":["json","serialization","codecs","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-12T12:17:11.808Z"}