gchq/CyberChef · error · OperationError

Invalid JPath expression: ${err.message}

Error message

Invalid JPath expression: ${err.message}

What it means

Thrown when the JSONPath query passed to the JPath operation is syntactically invalid or unsupported by the underlying JSONPath library. The query string is handed verbatim to JSONPath({path, json}); if the engine rejects the path syntax it throws, and that message is surfaced.

Source

Thrown at src/core/operations/JPathExpression.mjs:63

     * @returns {string}
     */
    run(input, args) {
        const [query, delimiter] = args;
        let results, jsonObj;

        try {
            jsonObj = JSON.parse(input);
        } catch (err) {
            throw new OperationError(`Invalid input JSON: ${err.message}`);
        }

        try {
            results = JSONPath({
                path: query,
                json: jsonObj
            });
        } catch (err) {
            throw new OperationError(`Invalid JPath expression: ${err.message}`);
        }

        return results.map(result => JSON.stringify(result)).join(delimiter);
    }

}

export default JPathExpression;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Verify the root token: JSONPath expressions start with '$' (or '@' in filters).
  2. Validate bracket/parenthesis balancing and filter syntax '?(...)' before running.
  3. Test the expression in a JSONPath playground using the same library CyberChef bundles (jsonpath-plus).
  4. If migrating from jq, rewrite the expression in JSONPath grammar, not jq grammar.

Example fix

// before: jq-style expression passed to JSONPath
chef.JPathExpression(json, { query: '.store.book[].title' });
// after: valid JSONPath
chef.JPathExpression(json, { query: '$.store.book[*].title' });
Defensive patterns

Strategy: try-catch

Validate before calling

function looksLikeJsonPath(q) {
  return typeof q === 'string' && /^[@$]/.test(q) &&
    (q.match(/\[/g)||[]).length === (q.match(/\]/g)||[]).length &&
    (q.match(/\(/g)||[]).length === (q.match(/\)/g)||[]).length;
}

Type guard

function isJsonPathExpression(q) {
  return typeof q === 'string' && q.length > 0 && /^[@$.*\[\]()]/.test(q);
}

Try / catch

try {
  return chef.JPathExpression(json, { query });
} catch (e) {
  if (/Invalid JPath expression/.test(e.message)) {
    throw new Error(`JSONPath syntax error - check root token '$' and brackets: ${e.message}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Malformed path: '$.store.book[' (unclosed bracket), 'store.book' (missing leading $ root), a filter with a syntax error like '?(@.price >)', or recursive-descent '..' used where the library does not support it. Mixing jq syntax ('.foo') into a JSONPath field also triggers it.

Common situations: Translating between jq and JSONPath syntax without adjusting grammar. Copying an expression from a different JSONPath implementation (there are several dialects). Typos in bracket notation. Using script/filter extensions the linked build does not support.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/4cda78a70ca7d3b6. Report an issue: GitHub.