zloirock/core-js · error · SyntaxError
Unexpected extra character: "${chr}" after the parsed data a
Error message
Unexpected extra character: "${chr}" after the parsed data at: ${endIndex} What it means
This SyntaxError is thrown by the core-js JSON.parse polyfill ($parse) when, after successfully parsing a complete JSON value, additional non-whitespace characters remain in the input. Native JSON.parse reports this as 'Unexpected non-whitespace character after JSON'; core-js names the offending character and index explicitly.
Source
Thrown at packages/core-js/modules/es.json.parse.js:46
var exec = uncurryThis(/./.exec);
var push = uncurryThis([].push);
var IS_DIGIT = /^\d$/;
var IS_NON_ZERO_DIGIT = /^[1-9]$/;
var IS_NUMBER_START = /^[\d-]$/;
var IS_WHITESPACE = /^[\t\n\r ]$/;
var PRIMITIVE = 0;
var OBJECT = 1;
var $parse = function (source, reviver) {
source = toString(source);
var context = new Context(source, 0);
var root = context.parse();
var value = root.value;
var endIndex = context.skip(IS_WHITESPACE, root.end);
if (endIndex < source.length) {
throw new SyntaxError('Unexpected extra character: "' + at(source, endIndex) + '" after the parsed data at: ' + endIndex);
}
return isCallable(reviver) ? internalize({ '': value }, '', reviver, root) : value;
};
var internalize = function (holder, name, reviver, node) {
var val = holder[name];
var unmodified = node && val === node.value;
var context = unmodified && typeof node.source == 'string' ? { source: node.source } : {};
var elementRecordsLen, keys, len, i, P;
if (isObject(val)) {
var nodeIsArray = isArray(val);
var nodes = unmodified ? node.nodes : nodeIsArray ? [] : {};
if (nodeIsArray) {
elementRecordsLen = nodes.length;
len = lengthOfArrayLike(val);
for (i = 0; i < len; i++) {
internalizeProperty(val, i, internalize(val, '' + i, reviver, i < elementRecordsLen ? nodes[i] : undefined));
}View on GitHub (pinned to 84e45fba09)
Solutions
- Trim the input and ensure it contains exactly one JSON document; strip trailing garbage before parsing
- For multiple JSON documents, split on newlines and JSON.parse each line (NDJSON handling)
- Inspect the reported endIndex/at character to find where the extra content starts and fix the producer of the string
- If the response may contain HTML error pages, check Content-Type and status before parsing
Example fix
// before
const data = JSON.parse(rawBody);
// after
const data = JSON.parse(rawBody.trim());
// or for NDJSON:
const items = rawBody.trim().split('\n').map(line => JSON.parse(line)); Defensive patterns
Strategy: try-catch
Validate before calling
function isSingleJsonDocument(s) {
if (typeof s !== 'string') return false;
try { JSON.parse(s); return true; } catch { return false; }
}
// cheap pre-check: only whitespace may follow the last plausible terminator
function looksLikeSingleJson(s) {
const t = s.trim();
return /^[\[{"\-0-9tfn]/.test(t) && /\}$|\]$|"$|\d$|e$|l$/.test(t);
} Type guard
function parsesAsJson(v) {
if (typeof v !== 'string') return false;
try { JSON.parse(v); return true; } catch { return false; }
} Try / catch
let value;
try {
value = JSON.parse(raw);
} catch (e) {
if (e instanceof SyntaxError && /extra character/.test(e.message)) {
const idx = Number(e.message.match(/at: (\d+)$/)?.[1] ?? NaN);
value = JSON.parse(raw.slice(0, idx)); // salvage the first document
} else throw e;
} Prevention
- Trim whitespace and strip BOM before parsing
- Check response Content-Type is application/json and status is 2xx before parsing
- Treat multi-document payloads as NDJSON: split on newlines and parse each line
- Never concatenate JSON documents into one string
When it happens
Trigger: Calling JSON.parse() with a string like '{"a":1} {"b":2}', '{"a":1};', a JSON body with trailing garbage, or concatenated JSON documents (NDJSON passed as one string). The check is endIndex < source.length after skipping whitespace past root.end.
Common situations: Server responses that append debug output or HTML after JSON (e.g. PHP warnings before/after the body); concatenating multiple JSON objects instead of parsing line-by-line; trailing semicolons or commas copied from code; reading a file that contains multiple JSON documents.
Related errors
- Unexpected character: "${chr}" at: ${i}
- Unexpected character after padding
- Unterminated object at: ${i}
- Unterminated array at: ${i}
- Failed to parse number at: ${i}
AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30).
Data as JSON: /api/errors/6979ebbd3e4cd44b.
Report an issue: GitHub.