gchq/CyberChef · error · OperationError

Could not encode JSON to MessagePack: ${err}

Error message

Could not encode JSON to MessagePack: ${err}

What it means

To MessagePack serialises a JSON value using notepack.encode(). If the input is not serialisable by MessagePack — circular references, unsupported types, or excessively nested/deep structures — notepack throws, and the error is rewrapped with the underlying message.

Source

Thrown at src/core/operations/ToMessagePack.mjs:47

        this.args = [];
    }

    /**
     * @param {JSON} input
     * @param {Object[]} args
     * @returns {ArrayBuffer}
     */
    run(input, args) {
        try {
            if (isWorkerEnvironment()) {
                return notepack.encode(input);
            } else {
                const res = notepack.encode(input);
                // Safely convert from Node Buffer to ArrayBuffer using the correct view of the data
                return (new Uint8Array(res)).buffer;
            }
        } catch (err) {
            throw new OperationError(`Could not encode JSON to MessagePack: ${err}`);
        }
    }

}

export default ToMessagePack;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the input is plain JSON-serialisable data (objects, arrays, strings, numbers, booleans, null).
  2. Remove circular references and non-serialisable types (BigInt, functions, Symbols).
  3. Inspect the appended `err` in the message for the exact notepack failure.

Example fix

// before: input has a circular ref  -> notepack throws -> OperationError
// after:  input = { "a": 1, "b": [2,3] }  (plain JSON) -> encodes cleanly
Defensive patterns

Strategy: type-guard

Validate before calling

function isPlainJson(v, seen = new WeakSet()) {
  if (v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean") return true;
  if (typeof v !== "object" || typeof v === "function" || typeof v === "symbol") return false;
  if (seen.has(v)) return false; seen.add(v);
  return Array.isArray(v) ? v.every(x => isPlainJson(x, seen)) : Object.values(v).every(x => isPlainJson(x, seen));
}

Type guard

const isMessagePackable = v => isPlainJson(v);

Try / catch

try { toMessagePack(jsonInput, []); }
catch (e) { if (/Could not encode/.test(e.message)) { jsonInput = JSON.parse(JSON.stringify(jsonInput)); } else throw e; }

Prevention

When it happens

Trigger: Passing a JSON structure that contains circular references, BigInt values, functions, Symbols, or other types MessagePack cannot encode; or a structure that exceeds notepack's recursion/size limits.

Common situations: Chaining To MessagePack after an operation that produced an object with cycles; feeding hand-built JSON with non-serialisable members; very large nested payloads.

Related errors


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