larksuite/cli · error
jq: marshal result: %w
Error message
jq: marshal result: %w
What it means
This error wraps a failure from json.Marshal when applyJQ tries to re-serialize the jq-filtered result value back into JSON before returning it as a json.RawMessage. It only fires after the jq program compiled and executed successfully, but its output value cannot be marshaled — which in practice means the filter produced a value containing non-serializable data (e.g. NaN/Inf numbers or an invalid number produced by the jq evaluation). The 'jq: ' prefix groups all jq-related failures for this consumer.
Source
Thrown at internal/event/consume/jq.go:48
// applyJQ returns (nil, nil) when the expression filters out the event (e.g. select).
func applyJQ(code *gojq.Code, data json.RawMessage) (json.RawMessage, error) {
var input interface{}
if err := json.Unmarshal(data, &input); err != nil {
return nil, fmt.Errorf("jq: unmarshal input: %w", err)
}
iter := code.Run(input)
v, ok := iter.Next()
if !ok {
return nil, nil
}
if err, isErr := v.(error); isErr {
return nil, fmt.Errorf("jq: %w", err)
}
result, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("jq: marshal result: %w", err)
}
return json.RawMessage(result), nil
}
View on GitHub (pinned to 7fd6ef3c07)
Solutions
- Inspect the jq filter expression for arithmetic that can yield NaN/Infinity (division by zero, log of negatives) and guard it, e.g. '.x / (.y // 1)' or 'if .y == 0 then 0 else .x / .y end'.
- Print the filter output with a simpler filter (e.g. '.') to see which field is unserializable.
- Update or pin the jq evaluation dependency so numeric edge cases produce JSON-safe values.
- If the value legitimately cannot be JSON-encoded, return an empty/placeholder RawMessage instead of failing the whole consume step.
Example fix
// before filter := ".count / .total" // if total == 0 the result is +Inf and json.Marshal fails // after filter := "if .total == 0 then 0 else .count / .total end"
Defensive patterns
Strategy: validation
Validate before calling
// Guard the filter output before returning it as RawMessage
func isJSONSafe(v interface{}) bool {
b, err := json.Marshal(v)
return err == nil && json.Valid(b)
}
if !isJSONSafe(result) {
// substitute a safe default or surface a domain error before applyJQ does
} Type guard
func safeNumber(f float64) (float64, bool) {
if math.IsNaN(f) || math.IsInf(f, 0) {
return 0, false
}
return f, true
} Prevention
- Write jq filters that cannot divide by zero; use '// 1' or if/else guards.
- Sanity-check new filters with sample event payloads before deploying them to the consumer.
- Keep the jq evaluation library pinned and review its changelog for numeric handling fixes.
- Unit-test filters with edge-case payloads (zeros, negatives, empty objects).
When it happens
Trigger: Calling applyJQ with a compiled jq program whose evaluation result is a Go value that json.Marshal rejects (NaN/Inf floats, unsupported types injected via the jq value bridge); raised via fmt.Errorf('jq: marshal result: %w', err) at internal/event/consume/jq.go:48.
Common situations: JQ filter expressions that compute arithmetic producing NaN or division by zero (e.g. '.x / 0'), filters returning huge floats beyond JSON range, or custom value conversions feeding non-marshalable values into the result.
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
- %s has invalid const: %w
- Invalid column: {column!r}
- Invalid column index: {index}
- anchor outside sheet: {position!r}
- invalid chart size: {size!r}
AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04).
Data as JSON: /api/errors/c020d60073bc6a46.
Report an issue: GitHub.