karatelabs/karate · error

Unexpected end of JSON input in array

Error message

Unexpected end of JSON input in array

What it means

Thrown by JsonParser.parseArray when the parser reaches end of input while inside a JSON array, before finding the closing ']'. After parsing an element and skipping whitespace, the input ends, so the array is unterminated and the parse fails per RFC 8259.

Solutions

  1. Inspect the raw string being parsed and append the missing ']' (and finish any half-added element).
  2. Validate the payload with a JSON linter before parsing.
  3. If read from a file or network, ensure the full content was read (check for truncation/stream limits).
  4. Fix dynamic construction so the closing bracket is always emitted.

Example fix

// before
var list = karate.fromJson('[1, 2, 3');
// after
var list = karate.fromJson('[1, 2, 3]');
Defensive patterns

Strategy: validation

Validate before calling

static boolean isCompleteArray(String json) {
    String t = json == null ? "" : json.trim();
    return t.startsWith("[") && t.endsWith("]") && t.length() > 1;
}
// usage
if (!isCompleteArray(body)) throw new IllegalArgumentException("Truncated JSON array");

Type guard

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

Try / catch

try {
    List<Object> list = Json.parseArray(raw);
} catch (JsonSyntaxException e) {
    log.error("Unterminated JSON array, raw=[{}]", raw);
    return Collections.emptyList(); // or rethrow with context
}

Prevention

When it happens

Trigger: Parsing input like '[1,2' or '["a",' — an array whose ']' is missing before the end of the string.

Common situations: Truncated API response bodies, JSON arrays cut off while streaming/reading a file, log capture that clipped the payload, dynamic array building that forgot the closing bracket.

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/63e7cebc774ae4b4. Report an issue: GitHub.

Appendix: source

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

                throw syntaxError("Expected ',' or '}' in object");
            }
        }

        private List<Object> parseArray() {
            // we know s.charAt(pos) == '['
            pos++;
            List<Object> list = new ArrayList<>();
            skipWs();
            if (pos < len && s.charAt(pos) == ']') {
                pos++;
                return list;
            }
            while (true) {
                skipWs();
                list.add(parseValue());
                skipWs();
                if (pos >= len) {
                    throw syntaxError("Unexpected end of JSON input in array");
                }
                char c = s.charAt(pos);
                if (c == ',') {
                    pos++;
                    // trailing-comma rejected: the next parseValue() lands on
                    // ']' and parseValue's default branch throws.
                    continue;
                }
                if (c == ']') {
                    pos++;
                    return list;
                }
                throw syntaxError("Expected ',' or ']' in array");
            }
        }

        private String parseString() {
            // we know s.charAt(pos) == '"'

View on GitHub (pinned to a22eb90246)