{"record":{"id":"fd2bbca57b53e13d","repo":"gchq/CyberChef","slug":"could-not-encode-json-to-messagepack-err","errorCode":null,"errorMessage":"Could not encode JSON to MessagePack: ${err}","messagePattern":"Could not encode JSON to MessagePack: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToMessagePack.mjs","lineNumber":47,"sourceCode":"        this.args = [];\n    }\n\n    /**\n     * @param {JSON} input\n     * @param {Object[]} args\n     * @returns {ArrayBuffer}\n     */\n    run(input, args) {\n        try {\n            if (isWorkerEnvironment()) {\n                return notepack.encode(input);\n            } else {\n                const res = notepack.encode(input);\n                // Safely convert from Node Buffer to ArrayBuffer using the correct view of the data\n                return (new Uint8Array(res)).buffer;\n            }\n        } catch (err) {\n            throw new OperationError(`Could not encode JSON to MessagePack: ${err}`);\n        }\n    }\n\n}\n\nexport default ToMessagePack;\n","sourceCodeStart":29,"sourceCodeEnd":54,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToMessagePack.mjs#L29-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"Chaining To MessagePack after an operation that produced an object with cycles; feeding hand-built JSON with non-serialisable members; very large nested payloads.","solutions":["Ensure the input is plain JSON-serialisable data (objects, arrays, strings, numbers, booleans, null).","Remove circular references and non-serialisable types (BigInt, functions, Symbols).","Inspect the appended `err` in the message for the exact notepack failure."],"exampleFix":"// before: input has a circular ref  -> notepack throws -> OperationError\n// after:  input = { \"a\": 1, \"b\": [2,3] }  (plain JSON) -> encodes cleanly","handlingStrategy":"type-guard","validationCode":"function isPlainJson(v, seen = new WeakSet()) {\n  if (v === null || typeof v === \"string\" || typeof v === \"number\" || typeof v === \"boolean\") return true;\n  if (typeof v !== \"object\" || typeof v === \"function\" || typeof v === \"symbol\") return false;\n  if (seen.has(v)) return false; seen.add(v);\n  return Array.isArray(v) ? v.every(x => isPlainJson(x, seen)) : Object.values(v).every(x => isPlainJson(x, seen));\n}","typeGuard":"const isMessagePackable = v => isPlainJson(v);","tryCatchPattern":"try { toMessagePack(jsonInput, []); }\ncatch (e) { if (/Could not encode/.test(e.message)) { jsonInput = JSON.parse(JSON.stringify(jsonInput)); } else throw e; }","preventionTips":["Strip circular references via JSON.parse(JSON.stringify(x)) before encoding.","Avoid BigInt/functions/Symbols in the payload.","Keep nesting depth reasonable for notepack."],"tags":["messagepack","serialization","json","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}