jaegertracing/jaeger · error
failed marshalling HTTP response to JSON: %w
Error message
failed marshalling HTTP response to JSON: %w
What it means
The HTTP API's JSON marshaler closure marshals the response value first, then writes it; a marshal failure is wrapped as "failed marshalling HTTP response to JSON". It distinguishes encoding failures from write failures so operators know the response never made it onto the wire as valid JSON.
Source
Thrown at cmd/jaeger/internal/extension/jaegerquery/internal/json_marshaler.go:40
marshaler.Indent = prettyPrintIndent
}
return func(w io.Writer, response any) error {
return marshaler.Marshal(w, response.(proto.Message))
}
}
// newStructJSONMarshaler returns a marshaler that uses the built-in encoding/json package for marshaling basic structs to JSON.
func newStructJSONMarshaler(prettyPrint bool) jsonMarshaler {
marshaler := json.Marshal
if prettyPrint {
marshaler = func(v any) ([]byte, error) {
return json.MarshalIndent(v, "", prettyPrintIndent)
}
}
return func(w io.Writer, response any) error {
resp, err := marshaler(response)
if err != nil {
return fmt.Errorf("failed marshalling HTTP response to JSON: %w", err)
}
_, err = w.Write(resp)
return err
}
}
View on GitHub (pinned to 806f444784)
Solutions
- Read the wrapped cause to identify the offending field/type in the response struct.
- Sanitize or convert unsupported values (NaN/Inf to 0 or strings, custom types to Marshaler) before returning them from the handler.
- If caused by a plugin or forked code, fix that component's response types; otherwise file an issue with the endpoint and query that triggered it.
Example fix
// before
type resp struct { Duration float64 }
// after
type resp struct { Duration string } // e.g. "1.5s" instead of a float that may be NaN Defensive patterns
Strategy: type-guard
Validate before calling
if err := json.Marshal(response); err != nil {
return fmt.Errorf("response not JSON-serializable: %w", err)
}
// run on handler output before returning it to writeJSON Type guard
func jsonSafe(v any) bool {
_, err := json.Marshal(v)
return err == nil
} Try / catch
resp, err := client.Do(req) // server-side: a 500 with message 'failed marshalling HTTP response to JSON' means a handler returned unserializable data; check the wrapped cause for the offending type/field.
Prevention
- Avoid NaN/Inf floats and chan/func fields in response structs.
- Add unit tests that marshal every handler's response payload.
- Sanitize values from plugins/storage before placing them in API responses.
When it happens
Trigger: A response value that cannot be serialized by encoding/json (e.g. an unsupported type such as chan or func, a NaN/Inf float, or a cyclic structure) reaches writeJSON on any query endpoint.
Common situations: A backend/storage plugin or custom query returns data containing values json.Marshal cannot handle; pretty-print mode using MarshalIndent hitting the same on large/odd payloads; version drift introducing a new field type.
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
- failed writing HTTP response: %w
- access denied
- no http.RoundTripper provided
- bad request
- unmarshalling ElasticSearch documents failed
AI-assisted analysis of jaegertracing/jaeger@806f444784 (2026-09-01).
Data as JSON: /api/errors/6f4cb51d79762268.
Report an issue: GitHub.