gchq/CyberChef · error · OperationError
Unable to parse input as JSON.\n${err}
Error message
Unable to parse input as JSON.\n${err} What it means
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.
Source
Thrown at src/core/operations/JSONBeautify.mjs:63
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
if (!input) return "";
const [indentStr, sortBool] = args;
let json = null;
try {
json = JSON5.parse(input);
} catch (err) {
throw new OperationError("Unable to parse input as JSON.\n" + err);
}
if (sortBool) json = sortKeys(json);
return JSON.stringify(json, null, indentStr);
}
/**
* Adds various dynamic features to the JSON blob
*
* @param {string} data
* @param {Object[]} args
* @returns {html}
*/
present(data, args) {
const formatted = args[2];
if (!formatted) return Utils.escapeHtml(data);
View on GitHub (pinned to 4290ea7539)
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.
Example fix
// before: non-JSON5 input
chef.JSONBeautify('<html>...</html>');
// after: feed valid JSON5
chef.JSONBeautify('{ a: 1, /* ok */ b: 2, }'); Defensive patterns
Strategy: validation
Validate before calling
function ensureJson5(input) {
try { JSON5.parse(input); return input; }
catch (e) { throw new Error(`Not JSON5: ${e.message}`); }
} Type guard
function isJson5String(s) {
if (typeof s !== 'string' || s.length === 0) return false;
try { JSON5.parse(s); return true; } catch { return false; }
} Try / catch
try {
return chef.JSONBeautify(input, { indentStr: ' ', sortBool: false });
} catch (e) {
if (/Unable to parse input as JSON/.test(e.message)) throw new Error('Provide JSON/JSON5 text to beautify');
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Invalid input JSON: ${err.message}
- Invalid input JSON: ${err.message}
- ${err}
- Error: Invalid Base64 input length (${data.length}). Cannot
- Error: Base64 padding character (${pad}) not used in the cor
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/7ff3a86b147da7cc.
Report an issue: GitHub.