kubernetes/kops · error
error marshaling manifest to json: %w
Error message
error marshaling manifest to json: %w
What it means
Object.MarshalJSON marshals the object's underlying generic data map with encoding/json and wraps failures with this error. It exists so kubemanifest.Object can be embedded into other JSON structures; a failure means the data map contains values json.Marshal cannot encode.
Source
Thrown at pkg/kubemanifest/manifest.go:139
yamls = append(yamls, y)
}
return bytes.Join(yamls, yamlSeparator), nil
}
func (m *Object) ToYAML() ([]byte, error) {
b, err := yaml.Marshal(m.data)
if err != nil {
return nil, fmt.Errorf("error marshaling manifest to yaml: %w", err)
}
return b, nil
}
func (m *Object) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(m.data)
if err != nil {
return nil, fmt.Errorf("error marshaling manifest to json: %w", err)
}
return b, nil
}
func (m *Object) accept(visitor Visitor) error {
err := visit(visitor, m.data, []string{}, func(v interface{}) {
klog.Fatal("cannot mutate top-level data")
})
return err
}
// IsEmptyObject checks if the object has no keys set (i.e. `== {}`)
func (m *Object) IsEmptyObject() bool {
return len(m.data) == 0
}
// Kind returns the kind field of the object, or "" if it cannot be found or is invalid
func (m *Object) Kind() string {View on GitHub (pinned to 4c8573c808)
Solutions
- Inspect the data map for non-JSON-encodable values (funcs, channels, cycles) and remove/convert them
- Use the wrapped error to locate the offending value
- Re-parse the manifest from source YAML to get a clean data map
- If custom values are needed, give them MarshalJSON implementations
Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := json.Marshal(obj.Data()); err != nil {
return fmt.Errorf("object not JSON-encodable: %w", err)
}
Try / catch
out, err := json.Marshal(structWithEmbeddedObject)
if err != nil && strings.Contains(err.Error(), "marshaling manifest to json") {
return fmt.Errorf("embedded manifest object is not JSON-encodable: %w", err)
}
Prevention
- Ensure data map values are JSON encodable before embedding Object in structs
- Give custom types MarshalJSON methods
- Avoid cyclic references in mutated manifests
- JSON round-trip test objects before serialization
When it happens
Trigger: Using kubemanifest.Object as a field in a struct being json.Marshal'ed (e.g. addon manifests serialized into other output) while its data contains unencodable values such as channels, funcs, or cyclic references.
Common situations: Embedding parsed manifests in JSON output/debug dumps; programmatic mutation injecting Go values that aren't JSON encodable.
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
- marshalling nodeupConfig: %w
- error marshaling into JSON: %v
- error building annotation patch: %v
- building node patch: %w
- error building node patch: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/6dd42db34539026e.
Report an issue: GitHub.