kubernetes/kops · error
error marshaling json: %v
Error message
error marshaling json: %v
What it means
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.
Source
Thrown at cmd/kops/get.go:117
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil
}
// obj must be a pointer to a marshalable object
func marshalYaml(obj runtime.Object) ([]byte, error) {
y, err := kopscodecs.ToVersionedYaml(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling yaml: %v", err)
}
return y, nil
}
// obj must be a pointer to a marshalable object
func marshalJSON(obj runtime.Object) ([]byte, error) {
j, err := kopscodecs.ToVersionedJSON(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling json: %v", err)
}
return j, nil
}
View on GitHub (pinned to 4c8573c808)
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
Example fix
// before j, err := kopscodecs.ToVersionedJSON(someUnregisteredObject) // after scheme := runtime.NewScheme() api.AddToScheme(scheme) // ensure kind is registered j, err := kopscodecs.ToVersionedJSON(obj)
Defensive patterns
Strategy: validation
Validate before calling
// before calling kopscodecs.ToVersionedJSON
if obj == nil || reflect.ValueOf(obj).Kind() != reflect.Ptr {
return fmt.Errorf("marshalJSON: obj must be a non-nil pointer")
}
kinds, _, err := scheme.ObjectKinds(obj)
if err != nil || len(kinds) == 0 {
return fmt.Errorf("object type %T not registered in scheme: %w", obj, err)
} Type guard
func isMarshalable(obj runtime.Object) bool {
if obj == nil { return false }
_, _, err := scheme.ObjectKinds(obj)
return err == nil
} Try / catch
j, err := marshalJSON(obj)
if err != nil {
var unreg *runtime.NotRegisteredErr
if errors.As(err, &unreg) {
// register kind in scheme and retry
}
return fmt.Errorf("error marshaling json: %v", err)
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
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 writing json to stdout: %v
- unable to marshal JSON: %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/a5244ef7bdac639e.
Report an issue: GitHub.