karatelabs/karate · error

Unexpected end of JSON input in object

Error message

Unexpected end of JSON input in object

What it means

This is a syntax error thrown by Karate's hand-written JSON parser (JsonParser.parseObject). After parsing a key/value pair, the parser skipped whitespace and reached the end of the input before seeing the ',' separator or the '}' that closes the object — meaning the JSON text stops mid-object. The library throws it because a JSON object must be closed for the parse to be well-formed per RFC 8259.

Solutions

  1. Print/inspect the exact JSON string being parsed and add the missing '}' (and any half-written pair) before parsing.
  2. Validate the payload with any strict JSON linter or JSON.parse equivalent before feeding it to Karate.
  3. If the JSON comes from an HTTP call, check status code and read the full body (don't truncate) before parsing.
  4. If the JSON is built dynamically, fix the template/interpolation that drops the closing characters.

Example fix

// before
karate.configure('json', '{"name": "foo"'); // truncated
// after
karate.configure('json', '{"name": "foo"}');
Defensive patterns

Strategy: validation

Validate before calling

// Java: validate before parsing
static boolean isCompleteObject(String json) {
    String t = json.trim();
    return t.startsWith("{") && t.endsWith("}") && t.length() > 1;
}
// usage
if (!isCompleteObject(payload)) throw new IllegalArgumentException("Truncated JSON object: " + payload);

Type guard

static boolean looksLikeJsonObject(String s) {
    if (s == null) return false;
    String t = s.trim();
    return t.startsWith("{") && t.endsWith("}");
}

Try / catch

try {
    Map<String, Object> map = Json.parse(payload);
} catch (JsonSyntaxException e) {
    log.error("Malformed JSON object: {}", payload, e);
    throw new IllegalArgumentException("Invalid JSON payload", e);
}

Prevention

When it happens

Trigger: Calling karate JSON parsing (JsonParser.parse / parseValue → parseObject) on input like '{"a":1' or '{"a":1,"b"' — an object whose closing '}' (or the rest of a pair after a comma) is missing before end of input.

Common situations: Truncated HTTP response bodies read as JSON, config/feature files cut off mid-edit, string interpolation that dropped the tail of a JSON payload, copying JSON and losing the last characters, template rendering that swallowed the closing brace.

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/546c41eb7c3318e6. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:130

            while (true) {
                skipWs();
                if (pos >= len || s.charAt(pos) != '"') {
                    throw syntaxError("Expected string key in object");
                }
                String key = parseString();
                skipWs();
                if (pos >= len || s.charAt(pos) != ':') {
                    throw syntaxError("Expected ':' after object key");
                }
                pos++;
                skipWs();
                Object value = parseValue();
                // RFC 8259: behavior of duplicate keys is unspecified; we keep
                // the last value (matches json-smart parseKeepingOrder).
                map.put(key, value);
                skipWs();
                if (pos >= len) {
                    throw syntaxError("Unexpected end of JSON input in object");
                }
                char c = s.charAt(pos);
                if (c == ',') {
                    pos++;
                    // trailing-comma is invalid per spec: the next iteration
                    // demands a string key, which rejects e.g. {"a":1,}.
                    continue;
                }
                if (c == '}') {
                    pos++;
                    return map;
                }
                throw syntaxError("Expected ',' or '}' in object");
            }
        }

        private List<Object> parseArray() {
            // we know s.charAt(pos) == '['

View on GitHub (pinned to a22eb90246)