octobercms/october · error · Error
Broken JSON object.
Error message
Broken JSON object.
What it means
Thrown at the top of JsonParser.parseString (October CMS AJAX framework) when the input, after trimming, is empty. The lenient parser (oc.parseJSON) refuses empty strings because there is no JSON value to produce; attribute helpers usually brace-wrap empty values into '{}' before parsing, so this surfaces mainly on direct oc.parseJSON('') calls.
Source
Thrown at modules/system/assets/js/framework.js:2062
return value;
}
if (value.charAt(0) !== "{") {
value = "{" + value + "}";
}
try {
return this.parseJSON(value);
} catch (e) {
throw new Error("Error parsing the " + name + " attribute value. " + e);
}
}
static parseJSON(json) {
return JSON.parse(new _JsonParser().parseString(json));
}
// Private
parseString(str) {
str = str.trim();
if (!str.length) {
throw new Error("Broken JSON object.");
}
var result = "";
while (str && str[0] === ",") {
str = str.substr(1);
}
if (str[0] === '"' || str[0] === "'") {
if (str[str.length - 1] !== str[0]) {
throw new Error("Invalid string JSON object.");
}
var body = '"';
for (var i = 1; i < str.length; i++) {
if (str[i] === "\\") {
if (str[i + 1] === "'") {
body += str[i + 1];
} else {
body += str[i];
body += str[i + 1];
}View on GitHub (pinned to b608633a7e)
Solutions
- Skip parsing when the input is blank: if (!raw || !raw.trim()) return defaultValue
- Default missing attributes: oc.parseJSON(el.getAttribute('x') || '{}')
- Guard upstream so empty state produces '{}' instead of ''
Example fix
// before
const data = oc.parseJSON(input.value);
// after
const data = oc.parseJSON(input.value.trim() || '{}'); Defensive patterns
Strategy: validation
Validate before calling
function parseOr(raw, fallback) {
const s = (raw ?? '').trim();
return s ? oc.parseJSON(s) : fallback;
}
const data = parseOr(input.value, {}); Type guard
const isNonEmptyJsonText = (s) => typeof s === 'string' && s.trim().length > 0;
Try / catch
try { const d = oc.parseJSON(raw); } catch (e) { if (/Broken JSON object\./.test(e.message)) { /* empty input - use default */ } } Prevention
- Default blank inputs to '{}' before parsing
- Coerce missing attributes: el.getAttribute('x') || '{}'
- Treat empty state as a valid case in your data flow, not as parse input
When it happens
Trigger: oc.parseJSON('') or oc.parseJSON(' ') called directly; code doing oc.parseJSON(el.getAttribute('x')) where the attribute is absent (null coerces oddly) or blank; feeding a wiped-out variable into the parser.
Common situations: Dynamic values that can be empty by design (empty filter state) piped straight into parseJSON; getAttribute returning null/'' for a missing attribute; AJAX payloads whose data field is empty and then re-parsed.
Related errors
- Broken JSON boolean body near ${str}
- Error parsing the ${name} attribute value. ${e}
- Error parsing the ${name} attribute value. ${e}
- Broken JSON number body near ${body}
- Broken JSON ${str[pos] === "{" ? "object" : "array"} body ne
AI-assisted analysis of octobercms/october@b608633a7e (2026-08-21).
Data as JSON: /api/errors/2d06027bc52ddb4f.
Report an issue: GitHub.