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

  1. Ensure valid JSON reaches the operation (decode/parse upstream first).
  2. Validate with JSON.parse in your own code before invoking.
  3. Strip BOM and surrounding whitespace.
  4. 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

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


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