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

  1. Check for null/empty before parsing: if (str && str.length) return JSON.parse(str).
  2. Assign a default: const data = body ? JSON.parse(body) : {}.
  3. Fix the upstream source so it actually returns a JSON string (check API call, file read, env var).
  4. 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

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.

Related errors


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)