gchq/CyberChef · error · OperationError
Invalid input JSON: ${err.message}
Error message
Invalid input JSON: ${err.message} What it means
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.
Source
Thrown at src/core/operations/Jsonata.mjs:49
type: "text",
value: "string",
},
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
async run(input, args) {
const [query] = args;
let result, jsonObj;
try {
jsonObj = JSON.parse(input);
} catch (err) {
throw new OperationError(`Invalid input JSON: ${err.message}`);
}
try {
const expression = jsonata(query);
// Override built-in base64 functions which fail in Web Worker
// context where `window` is undefined. The jsonata library falls
// back to `global.Buffer` which also does not exist in workers.
// `atob`/`btoa` are available in both browser and worker scopes.
expression.registerFunction("base64decode", (str) => {
if (typeof str === "undefined") return undefined;
return atob(str);
}, "<s-:s>");
expression.registerFunction("base64encode", (str) => {
if (typeof str === "undefined") return undefined;
return btoa(str);
}, "<s-:s>");
result = await expression.evaluate(jsonObj);
} catch (err) {View on GitHub (pinned to 4290ea7539)
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.
Example fix
// before: non-JSON input
chef.Jsonata('not json', { query: '$.' });
// after: valid JSON
chef.Jsonata(JSON.stringify({ a: 1 }), { query: '$.a' }); Defensive patterns
Strategy: validation
Validate before calling
function ensureJson(input) {
try { JSON.parse(input); return input; }
catch (e) { throw new Error(`Input is not valid JSON: ${e.message}`); }
} Type guard
function isValidJsonString(s) {
if (typeof s !== 'string') return false;
try { JSON.parse(s); return true; } catch { return false; }
} Try / catch
try {
return await chef.Jsonata(input, { query });
} catch (e) {
if (/Invalid input JSON/.test(e.message)) throw new Error('Pre-parse your data as JSON before JSONata');
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Invalid input JSON: ${err.message}
- Unable to parse input as JSON.\n${err}
- ${err}
- Invalid Jsonata Expression: ${err.message}
- Error: Invalid Base64 input length (${data.length}). Cannot
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/5c25a37fc98e5ca3.
Report an issue: GitHub.