gchq/CyberChef · error · OperationError
Invalid input JSON: ${err.message}
Error message
Invalid input JSON: ${err.message} What it means
Thrown by the JPath operation when the input text cannot be parsed as JSON. CyberChef runs JSON.parse on the raw string input before feeding it to the JSONPath query engine, so any non-JSON or malformed-JSON input aborts at that first step. The wrapped err.message is the underlying SyntaxError from the parser.
Source
Thrown at src/core/operations/JPathExpression.mjs:54
type: "binaryShortString",
value: "\\n"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @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
- Prepend a JSON-parse or a decode operation (e.g. From Base64) so the data reaching JPath is valid JSON.
- Validate the input with JSON.parse in a prior step or in your own code before invoking the operation.
- Strip a leading BOM (\uFEFF) and any surrounding whitespace/non-JSON wrapper.
- Run the input through JSONBeautify first to surface the exact parse location.
Example fix
// before: input is raw text
chef.JPathExpression('not json', { query: '$.*' });
// after: ensure valid JSON first
const safe = JSON.stringify({ a: 1 });
chef.JPathExpression(safe, { 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 chef.JPathExpression(input, { query });
} catch (e) {
if (/Invalid input JSON/.test(e.message)) throw new Error('Pre-parse your data as JSON before JPath');
throw e;
} Prevention
- Always place a JSON-producing/decoding operation before JPath in a recipe.
- Validate JSON with JSON.parse before invoking the Node API.
- Strip BOM and stray whitespace from pasted input.
When it happens
Trigger: Calling JPathExpression.run() with a string that is not valid JSON: plain prose, XML, base64, partial/truncated JSON, single-quoted keys, trailing commas, a leading BOM, or an empty string (JSON.parse('') throws).
Common situations: Chaining JPath directly after an operation whose output is not JSON (e.g. From Hex still leaving bytes, or a text op). Pasting un-decoded data. Copying JSON that lost a bracket. Encoding artifacts (BOM, CRLF) from Windows editors.
Related errors
- Unable to parse input as JSON.\n${err}
- Invalid input JSON: ${err.message}
- ${err}
- Error: Invalid Base64 input length (${data.length}). Cannot
- Error: Base64 padding character (${pad}) not used in the cor
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/2782891591bc20ed.
Report an issue: GitHub.