stanfordnlp/CoreNLP · error · SyntaxError

Badly formed JSON string

Error message

Badly formed JSON string: ${json}

What it means

This is a JavaScript (Prototype-style String extension) JSON parser helper. evalJSON() strips any JSON filter wrapper, optionally validates with isJSON(), and evaluates the string. If evaluation throws (or sanitization check fails), it rethrows a SyntaxError with an inspected version of the offending string, meaning the input was not parseable as JSON.

Solutions

  1. Log this.inspect() / the raw string to see what was actually received before parsing
  2. Validate the string with .isJSON() or JSON.parse in a try block before calling evalJSON
  3. Check the HTTP status and content-type before parsing; handle error pages separately
  4. Use JSON.parse instead of eval-based evalJSON for safer, clearer errors

Example fix

// before
var data = xhr.responseText.evalJSON(true);
// after
var data;
try { data = xhr.responseText.evalJSON(true); }
catch (e) { console.warn('Non-JSON response:', xhr.responseText); data = null; }
Defensive patterns

Strategy: try-catch

Validate before calling

function safeEvalJSON(str) { return (str && str.isJSON && str.isJSON()) ? str.evalJSON() : null; }

Type guard

function looksLikeJSON(s) { return typeof s === 'string' && s.trim().charAt(0) !== '<' && s.isJSON(); }

Try / catch

try { data = responseText.evalJSON(true); } catch (e) { if (e instanceof SyntaxError) { log(responseText); data = fallbackValue; } else { throw e; } }

Prevention

When it happens

Trigger: Calling '...'.evalJSON() (optionally with sanitize=true) on a string that is not valid JSON — e.g. trailing commas, single quotes, unquoted keys, HTML instead of JSON, or a truncated response. With sanitize=true it also throws when json.isJSON() returns false.

Common situations: Parsing XHR/AJAX response bodies that are actually HTML error pages, empty strings, or JSONP wrappers; server returning non-JSON on error statuses; hand-built JSON strings with syntax mistakes.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/2cbdedbea07635cb. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/time/suservlet/prototype.js:499

  },

  unfilterJSON: function(filter) {
    return this.sub(filter || Prototype.JSONFilter, '#{1}');
  },

  isJSON: function() {
    var str = this;
    if (str.blank()) return false;
    str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
    return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
  },

  evalJSON: function(sanitize) {
    var json = this.unfilterJSON();
    try {
      if (!sanitize || json.isJSON()) return eval('(' + json + ')');
    } catch (e) { }
    throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
  },

  include: function(pattern) {
    return this.indexOf(pattern) > -1;
  },

  startsWith: function(pattern) {
    return this.indexOf(pattern) === 0;
  },

  endsWith: function(pattern) {
    var d = this.length - pattern.length;
    return d >= 0 && this.lastIndexOf(pattern) === d;
  },

  empty: function() {
    return this == '';
  },

View on GitHub (pinned to 1b7edd19c4)