octobercms/october · error · Error

Broken JSON syntax near ${key}

Error message

Broken JSON syntax near ${key}

What it means

Thrown by JsonParser.parseKey (October CMS AJAX framework) while scanning an object key inside the relaxed parser. Keys terminate at the closing quote, a space, or the ':' separator; if the scan consumes the whole input without any of these appearing, there is no key/value boundary and the error reports the key text accumulated so far.

Source

Thrown at modules/system/assets/js/framework.js:2199

        }
        throw new Error("Broken JSON array near " + result);
      }
    }
    parseKey(str, pos, quote) {
      var key = "";
      for (var i = pos; i < str.length; i++) {
        if (quote && quote === str[i]) {
          return key;
        } else if (!quote && (str[i] === " " || str[i] === ":")) {
          return key;
        }
        key += str[i];
        if (str[i] === "\\" && i + 1 < str.length) {
          key += str[i + 1];
          i++;
        }
      }
      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);

View on GitHub (pinned to b608633a7e)

Solutions

  1. Give the key a value separator: {foo: 1} instead of {foo}
  2. Close quoted keys properly: {'foo': 1}
  3. If the value ends mid-key, restore the truncated tail of the attribute

Example fix

<!-- before -->
<div data-request-data="{foo}">...</div>

<!-- after -->
<div data-request-data="{foo: 1}">...</div>
Defensive patterns

Strategy: try-catch

Validate before calling

function keysHaveSeparators(s) {
  const t = s.trim();
  if (t[0] !== '{') return true;
  // every unquoted key run must be followed by ':' before '}' or end
  return !/\{\s*[\w$]+\s*\}/.test(t) && !/\{\s*[\w$]+\s*$/.test(t);
}

Try / catch

try { const d = oc.parseJSON(raw); } catch (e) { if (/Broken JSON syntax near/.test(e.message)) console.error('Key missing its ':' separator:', e.message); }

Prevention

When it happens

Trigger: oc.parseJSON("{foo") - no colon ever arrives; oc.parseJSON("{foo}") - '}' is not a key terminator for an unquoted key, so the scan runs off the end; a quoted key whose closing quote is missing ({'foo: 1}).

Common situations: Object literals missing the colon ({foo} shorthand that the lenient parser does not accept); lost '='->':' conversions when migrating query-string syntax to JSON; missing closing quote on quoted keys; truncated attribute values ending mid-key.

Related errors


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