{"record":{"id":"2782891591bc20ed","repo":"gchq/CyberChef","slug":"invalid-input-json-err-message","errorCode":null,"errorMessage":"Invalid input JSON: ${err.message}","messagePattern":"Invalid input JSON: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/JPathExpression.mjs","lineNumber":54,"sourceCode":"                type: \"binaryShortString\",\n                value: \"\\\\n\"\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const [query, delimiter] = args;\n        let results, 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            results = JSONPath({\n                path: query,\n                json: jsonObj\n            });\n        } catch (err) {\n            throw new OperationError(`Invalid JPath expression: ${err.message}`);\n        }\n\n        return results.map(result => JSON.stringify(result)).join(delimiter);\n    }\n\n}\n\nexport default JPathExpression;\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/JPathExpression.mjs#L36-L72","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before: input is raw text\nchef.JPathExpression('not json', { query: '$.*' });\n// after: ensure valid JSON first\nconst safe = JSON.stringify({ a: 1 });\nchef.JPathExpression(safe, { 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 chef.JPathExpression(input, { query });\n} catch (e) {\n  if (/Invalid input JSON/.test(e.message)) throw new Error('Pre-parse your data as JSON before JPath');\n  throw e;\n}","preventionTips":["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."],"tags":["json","jpath","input-validation","parse"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}