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
- Go to the reported position and insert the missing ',' between array elements.
- Remove the stray character that is not ',' or ']'.
- Replace semicolons or other separators with commas.
- 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
- Use commas, never semicolons or spaces, between array elements.
- Format JSON in an editor that flags syntax errors while typing.
- Serialize arrays from code rather than writing them by hand.
- Validate user-pasted JSON with JSON.parse/linter before accepting it.
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
- Expected ',' or '}' in object
- Expected ':' after object key
- Expected string key in object
- Invalid literal — expected 'false'
- Invalid literal — expected 'null'
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)