{"record":{"id":"7ff3a86b147da7cc","repo":"gchq/CyberChef","slug":"unable-to-parse-input-as-json-n-err","errorCode":null,"errorMessage":"Unable to parse input as JSON.\\n${err}","messagePattern":"Unable to parse input as JSON\\.\\\\n(.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/JSONBeautify.mjs","lineNumber":63,"sourceCode":"            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        if (!input) return \"\";\n\n        const [indentStr, sortBool] = args;\n        let json = null;\n\n        try {\n            json = JSON5.parse(input);\n        } catch (err) {\n            throw new OperationError(\"Unable to parse input as JSON.\\n\" + err);\n        }\n\n        if (sortBool) json = sortKeys(json);\n\n        return JSON.stringify(json, null, indentStr);\n    }\n\n    /**\n     * Adds various dynamic features to the JSON blob\n     *\n     * @param {string} data\n     * @param {Object[]} args\n     * @returns {html}\n     */\n    present(data, args) {\n        const formatted = args[2];\n        if (!formatted) return Utils.escapeHtml(data);\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/JSONBeautify.mjs#L45-L81","documentation":"Thrown by JSON Beautify when JSON5.parse cannot interpret the input. JSON5 is more lenient than strict JSON (it allows trailing commas, comments, unquoted keys, hex numbers), so this fires only on input that is not even valid JSON5. Note the message concatenates the raw err object (\"...\\n\" + err), which coerces to the Error's toString form rather than err.message.","triggerScenarios":"Input that is not structured data: prose, binary, HTML, a single bare token. Also a genuinely broken JSON5 document (unbalanced braces that JSON5 cannot recover from). Empty input is short-circuited (returns \"\"), so an empty string does NOT trigger this.","commonSituations":"Pasting a JavaScript object literal that references a variable. Feeding output of a binary/text op straight into JSON Beautify. Encoding problems that inject stray bytes.","solutions":["Confirm the upstream operation actually emits JSON/JSON5 text.","Use From Hex / From Base64 if the data is encoded before beautifying.","Manually locate the failing offset indicated in the wrapped error text.","Switch to a stricter JSON.parse test to find the exact syntax error, then re-run JSON Beautify."],"exampleFix":"// before: non-JSON5 input\nchef.JSONBeautify('<html>...</html>');\n// after: feed valid JSON5\nchef.JSONBeautify('{ a: 1, /* ok */ b: 2, }');","handlingStrategy":"validation","validationCode":"function ensureJson5(input) {\n  try { JSON5.parse(input); return input; }\n  catch (e) { throw new Error(`Not JSON5: ${e.message}`); }\n}","typeGuard":"function isJson5String(s) {\n  if (typeof s !== 'string' || s.length === 0) return false;\n  try { JSON5.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  return chef.JSONBeautify(input, { indentStr: '  ', sortBool: false });\n} catch (e) {\n  if (/Unable to parse input as JSON/.test(e.message)) throw new Error('Provide JSON/JSON5 text to beautify');\n  throw e;\n}","preventionTips":["Confirm the upstream op emits JSON/JSON5 text.","Decode (From Hex/Base64) encoded data first.","Remember empty input is allowed and returns an empty string."],"tags":["json","json5","input-validation","parse"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}