{"record":{"id":"4899ae9f0a3293e2","repo":"gchq/CyberChef","slug":"unable-to-stringify-yaml-err","errorCode":null,"errorMessage":"Unable to stringify YAML: ${err}","messagePattern":"Unable to stringify YAML: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/JSONtoYAML.mjs","lineNumber":40,"sourceCode":"        this.name = \"JSON to YAML\";\n        this.module = \"Default\";\n        this.description = \"Format a JSON object into YAML\";\n        this.infoURL = \"https://en.wikipedia.org/wiki/YAML\";\n        this.inputType = \"JSON\";\n        this.outputType = \"string\";\n        this.args = [];\n    }\n\n    /**\n     * @param {JSON} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        try {\n            return dump(input);\n        } catch (err) {\n            throw new OperationError(\"Unable to stringify YAML: \" + err);\n        }\n    }\n\n}\n\nexport default JSONtoYAML;\n","sourceCodeStart":22,"sourceCodeEnd":47,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/JSONtoYAML.mjs#L22-L47","documentation":"Thrown by JSON to YAML when js-yaml's dump() cannot serialise the input. The operation expects an already-parsed JSON object (inputType JSON), and dump() fails on values YAML cannot represent: circular references, functions, symbols, or unsupported scalar types under the active schema.","triggerScenarios":"Passing an object with a circular reference (a.b = a). An object containing a function, BigInt, or class instance. Extremely deep nesting exceeding js-yaml's limits. Passing a primitive string/number where a parsed object is expected (dump of a string succeeds, so the failure is specifically on un-serialisable structure).","commonSituations":"Decoding JSON then mutating the result into a cyclic graph before conversion. Using a custom class instance instead of a plain object. Hand-constructed objects with non-data members.","solutions":["Ensure the input is a plain JSON-deserialisable object (no cycles, no functions).","Break circular references (e.g. with a replacer or by removing back-pointers) before dumping.","JSON.stringify then JSON.parse the object first to strip functions/symbols/prototypes.","Pass dump options (noRefs, schema) if calling js-yaml directly."],"exampleFix":"// before: cyclic object\nconst o = {}; o.self = o;\nchef.JSONtoYAML(o);\n// after: acyclic plain object\nconst clean = JSON.parse(JSON.stringify({ a: 1, b: [2, 3] }));\nchef.JSONtoYAML(clean);","handlingStrategy":"validation","validationCode":"function safeForYaml(obj) {\n  const seen = new WeakSet();\n  (function walk(v) {\n    if (v === null || typeof v !== 'object') return;\n    if (seen.has(v)) throw new Error('Circular reference detected');\n    seen.add(v);\n    Object.values(v).forEach(walk);\n  })(obj);\n  return JSON.parse(JSON.stringify(obj));\n}","typeGuard":"function isPlainSerializable(obj) {\n  const seen = new WeakSet();\n  function ok(v) {\n    if (v === null || typeof v !== 'object') return typeof v !== 'function';\n    if (seen.has(v)) return false;\n    seen.add(v);\n    return Object.values(v).every(ok);\n  }\n  return ok(obj);\n}","tryCatchPattern":"try {\n  return chef.JSONtoYAML(obj);\n} catch (e) {\n  if (/Unable to stringify YAML/.test(e.message))\n    throw new Error('Object has cycles/functions - strip them before YAML export');\n  throw e;\n}","preventionTips":["Pass only plain, acyclic objects.","JSON.parse(JSON.stringify(obj)) to drop functions/symbols/prototypes first.","Avoid class instances; use plain object literals."],"tags":["yaml","json","conversion","serialization"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}