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

  1. Skip parsing when the input is blank: if (!raw || !raw.trim()) return defaultValue
  2. Default missing attributes: oc.parseJSON(el.getAttribute('x') || '{}')
  3. 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

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


AI-assisted analysis of octobercms/october@b608633a7e (2026-08-21). Data as JSON: /api/errors/2d06027bc52ddb4f. Report an issue: GitHub.