{"record":{"id":"1b1fc4634ddaca5f","repo":"gchq/CyberChef","slug":"invalid-jsonata-expression-err-message","errorCode":null,"errorMessage":"Invalid Jsonata Expression: ${err.message}","messagePattern":"Invalid Jsonata Expression: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Jsonata.mjs","lineNumber":68,"sourceCode":"        }\n\n        try {\n            const expression = jsonata(query);\n            // Override built-in base64 functions which fail in Web Worker\n            // context where `window` is undefined. The jsonata library falls\n            // back to `global.Buffer` which also does not exist in workers.\n            // `atob`/`btoa` are available in both browser and worker scopes.\n            expression.registerFunction(\"base64decode\", (str) => {\n                if (typeof str === \"undefined\") return undefined;\n                return atob(str);\n            }, \"<s-:s>\");\n            expression.registerFunction(\"base64encode\", (str) => {\n                if (typeof str === \"undefined\") return undefined;\n                return btoa(str);\n            }, \"<s-:s>\");\n            result = await expression.evaluate(jsonObj);\n        } catch (err) {\n            throw new OperationError(\n                `Invalid Jsonata Expression: ${err.message}`\n            );\n        }\n\n        return JSON.stringify(result === undefined ? \"\" : result);\n    }\n}\n\nexport default JsonataQuery;\n","sourceCodeStart":50,"sourceCodeEnd":78,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Jsonata.mjs#L50-L78","documentation":"Thrown by JSONata when either the expression fails to compile (jsonata(query)) or its evaluation throws (expression.evaluate(jsonObj)). The catch wraps both compile-time and runtime JSONata errors into a single message.","triggerScenarios":"Compile errors: unbalanced parentheses, invalid JSONata syntax, undefined/typo'd function names. Runtime errors during evaluate: division by zero, type mismatches, referencing a function the sandbox does not expose, or errors raised by the expression's own $error() calls.","commonSituations":"Iterating JSONata syntax from another query language. Using a JSONata function not present in the bundled version. Expressions that assume a data shape the input does not match.","solutions":["Split compile vs evaluate: call jsonata(query) separately to isolate a syntax error from a runtime one.","Test the expression in the JSONata Exerciser with the same payload.","Guard runtime paths with $exists() and '?' optional-axis operators.","Avoid functions the Web Worker context does not expose (the operation already overrides base64)."],"exampleFix":"// before: unguarded access on missing field\n'$account.balance'\n// after: existence guard\n'($exists($account) ? $account.balance : 0)'","handlingStrategy":"try-catch","validationCode":"function compileJsonata(query) {\n  try { return jsonata(query); }\n  catch (e) { throw new Error(`JSONata compile error: ${e.message}`); }\n}","typeGuard":"function isCompilableJsonata(q) {\n  try { jsonata(q); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  return await chef.Jsonata(json, { query });\n} catch (e) {\n  if (/Invalid Jsonata Expression/.test(e.message))\n    throw new Error('Split compile vs evaluate and test in the JSONata Exerciser');\n  throw e;\n}","preventionTips":["Compile the expression separately to isolate syntax from runtime errors.","Guard runtime paths with $exists() and '?'.","Avoid functions not exposed in the Web Worker context."],"tags":["jsonata","json","query","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}