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

  1. Prepend a JSON-parse or a decode operation (e.g. From Base64) so the data reaching JPath is valid JSON.
  2. Validate the input with JSON.parse in a prior step or in your own code before invoking the operation.
  3. Strip a leading BOM (\uFEFF) and any surrounding whitespace/non-JSON wrapper.
  4. 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

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


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/2782891591bc20ed. Report an issue: GitHub.