karatelabs/karate · error · SyntaxError
Unexpected end of JSON input
Error message
Unexpected end of JSON input
What it means
JsonParser.parse throws this SyntaxError when the input string is null, matching how browsers report empty JSON bodies ('Unexpected end of JSON input'). Because a null input contains zero characters, the parser treats it as truncated JSON rather than a distinct null case.
Solutions
- Check for null/empty before parsing: if (str && str.length) return JSON.parse(str).
- Assign a default: const data = body ? JSON.parse(body) : {}.
- Fix the upstream source so it actually returns a JSON string (check API call, file read, env var).
- Catch the syntax error and treat empty input as a sentinel value where appropriate.
Example fix
// before
const cfg = JSON.parse(body); // body is null
// after
const cfg = body ? JSON.parse(body) : {}; Defensive patterns
Strategy: validation
Validate before calling
if (input == null || input.length === 0) throw new Error('cannot parse empty JSON input');
var value = JSON.parse(input); Type guard
function isNonEmptyString(s) { return typeof s === 'string' && s.length > 0; } Try / catch
try { return JSON.parse(input); } catch (e) { if (String(e).indexOf('Unexpected end of JSON input') !== -1) return null; throw e; } Prevention
- Always check body/file/env-var content is non-empty before JSON.parse
- Give 204/empty responses an explicit default value
- Initialize JSON-holding variables with a string, not null
- Log raw input when parse fails to spot empty sources early
When it happens
Trigger: JsonParser.parse(null) directly; in Karate JS, JSON.parse(variable) where the variable is null/empty — e.g. reading a response body or file that came back empty.
Common situations: HTTP responses with 204 No Content or empty bodies being fed to JSON.parse; empty config or data files read as empty strings/null; variable initialized to null but expected to hold a JSON string; upstream service returning no payload.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Cannot convert null or undefined to object
- Array.prototype.* called on null or undefined
- cannot destructure
- Converting circular structure to JSON
- Do not know how to serialize a BigInt
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/8c5118392cddba50.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:55
*
* <p>Numeric narrowing: integer values in {@code int} range → {@link Integer};
* integer values fitting in {@code long} → {@link Long}; larger integers →
* {@link BigInteger}; any literal with a fractional part or exponent →
* {@link Double}. See {@code JsonParserTest} for the pinned contract.
*
* <p>Designed to be allocated per call ({@link ParseState} is a private,
* non-static inner class); the {@code parse} entry point is thread-safe by
* construction — no shared mutable state.
*/
public final class JsonParser {
private JsonParser() {
// static utility
}
public static Object parse(String input) {
if (input == null) {
throw JsErrorException.syntaxError("Unexpected end of JSON input");
}
ParseState state = new ParseState(input);
state.skipWs();
Object value = state.parseValue();
state.skipWs();
if (state.pos != input.length()) {
throw state.syntaxError("Unexpected token after JSON value");
}
return value;
}
private static final class ParseState {
private final String s;
private final int len;
private int pos;
ParseState(String s) {View on GitHub (pinned to a22eb90246)