karatelabs/karate · error
Unterminated JSON string
Error message
Unterminated JSON string
What it means
Thrown by JsonParser.parseString when the closing double quote of a JSON string is never found — the input ends while still inside a string. Every JSON string must be terminated by '"', so a string that runs to end-of-input is malformed.
Solutions
- Inspect the string being parsed and add the missing closing '"'.
- Check for an unintended backslash before the closing quote ('\"' escapes it) and remove or double the backslash.
- Verify the payload wasn't truncated in transit — read the full response/file.
- Use a JSON serializer to produce strings so quoting is always balanced.
Example fix
// before
'{"name": "karate' // missing closing quote
// after
'{"name": "karate"}' Defensive patterns
Strategy: validation
Validate before calling
// Check quote balance (ignoring escapes) before parsing
static boolean hasUnterminatedString(String s) {
boolean inStr = false;
for (int i = 0; s != null && i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\\') { i++; continue; }
if (c == '"') inStr = !inStr;
}
return inStr;
} Try / catch
try {
return Json.parse(raw);
} catch (JsonSyntaxException e) {
if (e.getMessage().equals("Unterminated JSON string")) {
throw new IllegalArgumentException("Missing closing quote; raw=" + raw, e);
}
throw e;
} Prevention
- Verify the full payload arrived (length/status checks) before parsing.
- Watch for accidental \" sequences that swallow the intended closing quote.
- Use an editor with JSON syntax highlighting to spot unbalanced quotes.
- Prefer serializers for string content containing quotes.
When it happens
Trigger: Parsing input like '{"a": "value' — an opening quote with no matching closing quote before end of input, usually from truncation or a missing/escaped-away quote.
Common situations: Truncated HTTP bodies or file reads, an accidental '\"' that escaped the intended closing quote, template interpolation that dropped the quote, hand-edited JSON missing the final quote.
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
- Expected ':' after object key
- Expected ',' or ']' in array
- Expected ',' or '}' in object
- Expected string key in object
- Invalid literal — expected 'false'
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/8dfed80314c3aa53.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:234
break;
case 't':
sb.append('\t');
break;
case 'u':
sb.append(parseHex4());
break;
default:
throw syntaxError("Invalid escape '\\" + esc + "' in JSON string");
}
continue;
}
if (c < 0x20) {
throw syntaxError("Invalid control character in JSON string");
}
sb.append(c);
pos++;
}
throw syntaxError("Unterminated JSON string");
}
private char parseHex4() {
if (pos + 4 > len) {
throw syntaxError("Invalid \\u escape in JSON string");
}
int v = 0;
for (int i = 0; i < 4; i++) {
char h = s.charAt(pos + i);
int d;
if (h >= '0' && h <= '9') d = h - '0';
else if (h >= 'a' && h <= 'f') d = 10 + (h - 'a');
else if (h >= 'A' && h <= 'F') d = 10 + (h - 'A');
else throw syntaxError("Invalid hex digit '" + h + "' in \\u escape");
v = (v << 4) | d;
}
pos += 4;
return (char) v;View on GitHub (pinned to a22eb90246)