karatelabs/karate · error

Expected ',' or ']' in array

Error message

Expected ',' or ']' in array

What it means

Thrown by JsonParser.parseArray when the character following a complete array element is neither ',' (next element) nor ']' (end of array). The array contains a malformed separator or stray token where the grammar only allows ',' or ']'.

Solutions

  1. Go to the reported position and insert the missing ',' between array elements.
  2. Remove the stray character that is not ',' or ']'.
  3. Replace semicolons or other separators with commas.
  4. Generate arrays programmatically with a JSON serializer rather than by hand.

Example fix

// before
'[1, 2 3]'
// after
'[1, 2, 3]'
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate array element separators with a standard parser
try {
    new com.fasterxml.jackson.databind.ObjectMapper().readTree(json);
} catch (JsonProcessingException e) {
    throw new IllegalArgumentException("Invalid array separator: " + e.getOriginalMessage());
}

Type guard

static boolean hasBalancedArraySyntax(String s) {
    return s != null && s.trim().matches("\\[.*\\]");
} // combine with a real parse for full safety

Try / catch

try {
    Object list = Json.parse(raw);
} catch (JsonSyntaxException e) {
    throw new IllegalArgumentException("Malformed JSON array near: " + e.getMessage());
}

Prevention

When it happens

Trigger: Parsing input like '[1 2]' (missing comma between elements), '[1; 2]', or '[1 }]' — an unexpected character appears between elements.

Common situations: Hand-written arrays missing commas, pasting values from other languages (e.g. semicolon-separated), naive string building of arrays, editing generated fixtures by hand.

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

Appendix: source

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

            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) == '"'
            pos++;
            StringBuilder sb = new StringBuilder();
            while (pos < len) {
                char c = s.charAt(pos);
                if (c == '"') {
                    pos++;
                    return sb.toString();
                }
                if (c == '\\') {
                    pos++;
                    if (pos >= len) {
                        throw syntaxError("Unexpected end of JSON input in string escape");
                    }

View on GitHub (pinned to a22eb90246)