octobercms/october · error · Error
Broken JSON string body near ${body}
Error message
Broken JSON string body near ${body} What it means
October CMS's front-end framework ships a lenient JSON parser (JsonParser in framework.js, exposed as oc.parseJSON) that accepts JavaScript object-literal syntax — single quotes, unquoted keys, trailing commas — in data attributes such as data-request-data. Its getBody() method scans one value token; when a value starts with a quote it walks forward looking for the matching closing quote. This error means the input ended before that closing quote was found, so the string value token could never be completed.
Source
Thrown at modules/system/assets/js/framework.js:2217
throw new Error("Broken JSON syntax near " + key);
}
getBody(str, pos) {
if (str[pos] === '"' || str[pos] === "'") {
var body = str[pos];
for (var i = pos + 1; i < str.length; i++) {
if (str[i] === "\\") {
body += str[i];
if (i + 1 < str.length) body += str[i + 1];
i++;
} else if (str[i] === str[pos]) {
body += str[pos];
return {
originLength: body.length,
body
};
} else body += str[i];
}
throw new Error("Broken JSON string body near " + body);
}
if (str[pos] === "t") {
if (str.indexOf("true", pos) === pos) {
return {
originLength: "true".length,
body: "true"
};
}
throw new Error("Broken JSON boolean body near " + str.substr(0, pos + 10));
}
if (str[pos] === "f") {
if (str.indexOf("f", pos) === pos) {
return {
originLength: "false".length,
body: "false"
};
}
throw new Error("Broken JSON boolean body near " + str.substr(0, pos + 10));View on GitHub (pinned to b608633a7e)
Solutions
- Add the missing closing quote that matches the opening quote of the broken string value (the message shows everything scanned so far in 'near ...').
- Escape apostrophes inside single-quoted strings with a backslash (\'), or switch to double-quoted strings inside the attribute.
- If the value contains double quotes, swap the HTML attribute delimiters (data-request-data="..." to data-request-data='...') so the inner quotes survive HTML parsing.
- Reproduce in the browser console with oc.parseJSON(element.dataset.requestData) to see exactly which token the parser choked on.
Example fix
// before <button data-request="onSave" data-request-data="name: 'John's file">Save</button> // after <button data-request="onSave" data-request-data='name: "John's file"'>Save</button>
Defensive patterns
Strategy: try-catch
Validate before calling
// Before calling oc.parseJSON on an attribute value, confirm quotes terminate
function hasUnterminatedString(value) {
var inQuote = null;
for (var i = 0; i < value.length; i++) {
var ch = value[i];
if (inQuote) {
if (ch === '\\') { i++; continue; }
if (ch === inQuote) inQuote = null;
} else if (ch === '"' || ch === "'") {
inQuote = ch;
}
}
return inQuote !== null;
} Try / catch
try {
var data = oc.parseJSON(el.dataset.requestData);
} catch (e) {
console.error('Invalid data-request-data on', el, el.dataset.requestData, e.message);
return; // skip the request rather than break the page
} Prevention
- Standardize on double quotes inside data-* attribute values and single quotes for the HTML attribute itself.
- Escape user-supplied text (apostrophes, quotes) before interpolating it into attribute values server-side.
- Add a smoke test that runs oc.parseJSON over every data-request-data value in rendered fixtures.
When it happens
Trigger: An attribute value with an unterminated string, e.g. data-request-data="delay: '300"; an apostrophe inside a single-quoted value such as name: 'it's here' (the second ' closes the string early and the rest never terminates); a value truncated mid-string because a raw double quote inside a double-quoted HTML attribute cut the attribute off; a direct oc.parseJSON("{'key': 'unterminated}") call.
Common situations: Writing data-request-data or similar data-* attributes by hand in CMS partials; templating that interpolates user text (names with apostrophes) into attribute values; HTML produced by a WYSIWYG editor that normalizes quotes; migrating old jQuery .data('request-data', "...") strings into attributes.
Related errors
- Broken JSON boolean body near ${str}
- Broken JSON number body near ${body}
- Broken JSON ${str[pos] === "{" ? "object" : "array"} body ne
- Broken JSON body near ${str}
- Broken JSON boolean body near ${str}
AI-assisted analysis of octobercms/october@b608633a7e (2026-08-21).
Data as JSON: /api/errors/1af8464fe7f57dd9.
Report an issue: GitHub.