{"record":{"id":"5c25a37fc98e5ca3","repo":"gchq/CyberChef","slug":"invalid-input-json-err-message-5c25a3","errorCode":null,"errorMessage":"Invalid input JSON: ${err.message}","messagePattern":"Invalid input JSON: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Jsonata.mjs","lineNumber":49,"sourceCode":"                type: \"text\",\n                value: \"string\",\n            },\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    async run(input, args) {\n        const [query] = args;\n        let result, jsonObj;\n\n        try {\n            jsonObj = JSON.parse(input);\n        } catch (err) {\n            throw new OperationError(`Invalid input JSON: ${err.message}`);\n        }\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) {","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Jsonata.mjs#L31-L67","documentation":"Thrown by the JSONata operation when JSON.parse(input) fails on the raw string input. Identical pattern to the JPath parse error: CyberChef parses the text to an object before handing it to the JSONata expression evaluator.","triggerScenarios":"Non-JSON input string: prose, encoded bytes, partial JSON, trailing commas (JSONata's input here is strict JSON.parse, not JSON5), single-quoted keys, a BOM, or empty string.","commonSituations":"Chaining JSONata after a non-JSON op. Pasting data that lost structure. Strict-vs-lenient confusion (the expression language is lenient, but the input parse here is strict).","solutions":["Ensure valid JSON reaches the operation (decode/parse upstream first).","Validate with JSON.parse in your own code before invoking.","Strip BOM and surrounding whitespace.","Use JSONBeautify (JSON5) upstream only if the result is then strict-JSON compatible."],"exampleFix":"// before: non-JSON input\nchef.Jsonata('not json', { query: '$.' });\n// after: valid JSON\nchef.Jsonata(JSON.stringify({ a: 1 }), { query: '$.a' });","handlingStrategy":"validation","validationCode":"function ensureJson(input) {\n  try { JSON.parse(input); return input; }\n  catch (e) { throw new Error(`Input is not valid JSON: ${e.message}`); }\n}","typeGuard":"function isValidJsonString(s) {\n  if (typeof s !== 'string') return false;\n  try { JSON.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  return await chef.Jsonata(input, { query });\n} catch (e) {\n  if (/Invalid input JSON/.test(e.message)) throw new Error('Pre-parse your data as JSON before JSONata');\n  throw e;\n}","preventionTips":["Place a JSON-producing op before JSONata in a recipe.","Validate with JSON.parse first (the input parse is strict, not JSON5).","Strip BOM/whitespace from pasted data."],"tags":["json","jsonata","input-validation","parse"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}