{"record":{"id":"20459e47aab621f7","repo":"gchq/CyberChef","slug":"invalid-jq-expression-err-message","errorCode":null,"errorMessage":"Invalid jq expression: ${err.message}","messagePattern":"Invalid jq expression: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Jq.mjs","lineNumber":54,"sourceCode":"                type: \"boolean\",\n                value: false\n            },\n        ];\n    }\n\n    /**\n     * @param {JSON} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const [query, raw] = args;\n        let result;\n\n        try {\n            result = jq.json(input, query);\n        } catch (err) {\n            throw new OperationError(`Invalid jq expression: ${err.message}`);\n        }\n        if (raw && typeof result === \"string\") {\n            return result;\n        } else {\n            return JSON.stringify(result);\n        }\n    }\n\n}\n\nexport default Jq;\n","sourceCodeStart":36,"sourceCodeEnd":66,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Jq.mjs#L36-L66","documentation":"Thrown by the Jq operation when the jq-web engine raises an error compiling or evaluating the query. Input is already-parsed JSON (inputType 'JSON'); the failure is in the query itself or a runtime evaluation error, surfaced as err.message from the WASM-backed jq engine.","triggerScenarios":"Query syntax errors: misplaced pipes '|', bad object construction, invalid indices. Runtime errors during evaluation: iterating over null, dividing by zero, calling an undefined function. Using a jq feature the bundled jq-web build lacks.","commonSituations":"Translating between jq and JSONPath. Typing a filter against the wrong data shape. jq-web being an older WASM port lagging behind jq's feature set. Heavy queries hitting WASM limits.","solutions":["Test the query with desktop jq against the same input to isolate syntax vs engine limits.","Guard against null-iteration: use '?', '//', or 'map(...)' to avoid errors on missing fields.","Simplify the query to the smallest failing fragment and rebuild.","Confirm the input is the object Jq expects (parsed JSON), not a raw string."],"exampleFix":"// before: null-iteration error\njq.json(data, '.users[].name'); // .users missing\n// after: safe access\njq.json(data, '.users // [] | .[].name');","handlingStrategy":"try-catch","validationCode":"function safeJqQuery(q) {\n  if (typeof q !== 'string' || q.length === 0) throw new Error('Empty jq query');\n  const pairs = { ')': '(', ']': '[' };\n  const stack = [];\n  for (const ch of q) {\n    if (ch === '(' || ch === '[') stack.push(ch);\n    else if (pairs[ch]) { if (stack.pop() !== pairs[ch]) throw new Error('Unbalanced jq query'); }\n  }\n  if (stack.length) throw new Error('Unbalanced jq query');\n  return q;\n}","typeGuard":"function isBalancedJqQuery(q) {\n  try { safeJqQuery(q); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  return chef.Jq(jsonObj, { query, raw: false });\n} catch (e) {\n  if (/Invalid jq expression/.test(e.message))\n    throw new Error('Test the query with desktop jq; guard null-iteration with // and ?');\n  throw e;\n}","preventionTips":["Test queries with desktop jq against the same payload.","Guard null iteration with '?', '//', and 'map'.","Pass a parsed JSON object, not a raw string."],"tags":["jq","json","query","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}