{"record":{"id":"b0afa661729796ae","repo":"larksuite/cli","slug":"jq-unmarshal-input-w","errorCode":null,"errorMessage":"jq: unmarshal input: %w","messagePattern":"jq: unmarshal input: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/event/consume/jq.go","lineNumber":34,"sourceCode":"func CompileJQ(expr string) (*gojq.Code, error) {\n\tquery, err := gojq.Parse(expr)\n\tif err != nil {\n\t\treturn nil, errs.NewValidationError(errs.SubtypeInvalidArgument,\n\t\t\t\"invalid jq expression: %s\", err).WithParam(\"--jq\").WithCause(err)\n\t}\n\tcode, err := gojq.Compile(query)\n\tif err != nil {\n\t\treturn nil, errs.NewValidationError(errs.SubtypeInvalidArgument,\n\t\t\t\"jq compile error: %s\", err).WithParam(\"--jq\").WithCause(err)\n\t}\n\treturn code, nil\n}\n\n// applyJQ returns (nil, nil) when the expression filters out the event (e.g. select).\nfunc applyJQ(code *gojq.Code, data json.RawMessage) (json.RawMessage, error) {\n\tvar input interface{}\n\tif err := json.Unmarshal(data, &input); err != nil {\n\t\treturn nil, fmt.Errorf(\"jq: unmarshal input: %w\", err)\n\t}\n\n\titer := code.Run(input)\n\tv, ok := iter.Next()\n\tif !ok {\n\t\treturn nil, nil\n\t}\n\tif err, isErr := v.(error); isErr {\n\t\treturn nil, fmt.Errorf(\"jq: %w\", err)\n\t}\n\n\tresult, err := json.Marshal(v)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"jq: marshal result: %w\", err)\n\t}\n\treturn json.RawMessage(result), nil\n}\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/internal/event/consume/jq.go#L16-L52","documentation":"applyJQ must unmarshal the event's raw JSON payload into interface{} before running the compiled gojq expression; this error wraps the json.Unmarshal failure. The library throws it because a jq filter can only run on valid JSON — if the event payload is not parseable JSON, filtering cannot proceed and the event is rejected with this jq-prefixed error.","triggerScenarios":"json.Unmarshal(data, &input) fails inside applyJQ (internal/event/consume/jq.go:31-35): the event payload passed from processAndOutput is empty, truncated, non-JSON (e.g. plain text or binary), or contains a JSON syntax error such as trailing commas or single quotes.","commonSituations":"Upstream producer emitted a malformed or empty payload; a preceding pipeline stage corrupted or double-encoded the event; the jq filter was pointed at an event type whose body is not JSON; encoding mismatches (UTF-16/BOM bytes) from a non-Go producer.","solutions":["Inspect the offending event payload and fix the producer to emit valid JSON.","Validate the event body (json.Valid) before applying the jq filter and skip/route invalid events to a dead-letter path.","Check for double encoding: if data looks like \"\\\"{...}\\\"\", unmarshal once more before applying jq.","Verify the pipeline stage upstream of applyJQ isn't truncating or transforming the raw message."],"exampleFix":"// before: jq applied blindly to any payload\nout, err := applyJQ(code, data)\n// after: validate first\nif !json.Valid(data) {\n    return fmt.Errorf(\"event payload is not valid JSON: %q\", truncate(data))\n}\nout, err := applyJQ(code, data)","handlingStrategy":"validation","validationCode":"func ensureJSON(data json.RawMessage) error {\n    if len(bytes.TrimSpace(data)) == 0 {\n        return errors.New(\"empty payload\")\n    }\n    if !json.Valid(data) {\n        return fmt.Errorf(\"invalid JSON: %s\", truncate(data, 200))\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"out, err := applyJQ(code, data)\nif err != nil {\n    var uerr error\n    if strings.Contains(err.Error(), \"jq: unmarshal input\") {\n        deadLetter(event, err) // payload not JSON; skip filter\n        return nil\n    }\n    return err\n}","preventionTips":["Validate producer output with json.Valid in producer tests/CI.","Run json.Valid on events before jq in the pipeline and route invalid ones to a dead-letter queue.","Watch for double-encoded JSON when crossing language/transport boundaries."],"tags":["json","jq","validation","events"],"backgroundTag":"invalid-json-payload","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}