gchq/CyberChef · error · OperationError
Invalid jq expression: ${err.message}
Error message
Invalid jq expression: ${err.message} What it means
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.
Source
Thrown at src/core/operations/Jq.mjs:54
type: "boolean",
value: false
},
];
}
/**
* @param {JSON} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [query, raw] = args;
let result;
try {
result = jq.json(input, query);
} catch (err) {
throw new OperationError(`Invalid jq expression: ${err.message}`);
}
if (raw && typeof result === "string") {
return result;
} else {
return JSON.stringify(result);
}
}
}
export default Jq;
View on GitHub (pinned to 4290ea7539)
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.
Example fix
// before: null-iteration error jq.json(data, '.users[].name'); // .users missing // after: safe access jq.json(data, '.users // [] | .[].name');
Defensive patterns
Strategy: try-catch
Validate before calling
function safeJqQuery(q) {
if (typeof q !== 'string' || q.length === 0) throw new Error('Empty jq query');
const pairs = { ')': '(', ']': '[' };
const stack = [];
for (const ch of q) {
if (ch === '(' || ch === '[') stack.push(ch);
else if (pairs[ch]) { if (stack.pop() !== pairs[ch]) throw new Error('Unbalanced jq query'); }
}
if (stack.length) throw new Error('Unbalanced jq query');
return q;
} Type guard
function isBalancedJqQuery(q) {
try { safeJqQuery(q); return true; } catch { return false; }
} Try / catch
try {
return chef.Jq(jsonObj, { query, raw: false });
} catch (e) {
if (/Invalid jq expression/.test(e.message))
throw new Error('Test the query with desktop jq; guard null-iteration with // and ?');
throw e;
} Prevention
- Test queries with desktop jq against the same payload.
- Guard null iteration with '?', '//', and 'map'.
- Pass a parsed JSON object, not a raw string.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid Jsonata Expression: ${err.message}
- Invalid JPath expression: ${err.message}
- Unable to parse JSON to CSV: ${err.toString()}
- Could not encode JSON to MessagePack: ${err}
- Data is not a valid ${Dish.enumLookup(type)}: ${sample}
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/20459e47aab621f7.
Report an issue: GitHub.