karatelabs/karate · error · RuntimeException
Invalid get expression, missing ]:
Error message
Invalid get expression, missing ]:
What it means
The `get` keyword supports the `get[N] var.path` syntax for list indexing. The executor scans for the closing ']' to split the index from the remainder path; if the expression starts with 'get[' but contains no ']', the bracket is unbalanced and parsing cannot proceed, so this error is thrown with the raw expression.
Solutions
- Add the closing bracket: `get[0] myList`
- Prefer the bracket-on-variable form if clearer: `myList[0]` inside def/eval
- Check the step text was not truncated by templating or line-wrapping
- Verify no nested brackets are confusing the parser
Example fix
// before * def first = get[0 items // after * def first = get[0] items
Defensive patterns
Strategy: validation
Validate before calling
* if (!getExpr.startsWith('get[') || getExpr.indexOf(']') < 0) karate.fail('malformed get expression') Prevention
- Use the canonical form get[N] var.path with balanced brackets
- Prefer var[N] syntax which is harder to malform
- Lint step text for unbalanced brackets in generated tests
When it happens
Trigger: `* def x = get[0 foo` (missing closing bracket); nested/broken expressions where the bracket was consumed or mistyped (e.g. `get(0] foo`); truncated text from templating.
Common situations: Hand-editing get expressions and dropping the bracket; generating steps programmatically with malformed templates; confusing `get[0] list` with `get list[0]` syntax and mixing forms.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- configure requires '=' assignment:
- def requires '=' assignment
- fromString JSON parse failed
- fromString XML parse failed
- invalid destructuring: rest element cannot have an…
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/1f69ed2b5215f4a3.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/StepExecutor.java:3153
private Object evalCallOnceExpression(String callExpr) {
String tempVar = "__callOnceResult__" + System.nanoTime();
executeCallOnceWithResult(callExpr, tempVar);
return runtime.getVariable(tempVar);
}
/**
* Evaluates get[N] varname path or get varname path syntax.
* Examples: get[0] foo[*].name, get foo $..bar, get xml //xpath
*/
private Object evalGetExpression(String expr) {
int index = -1;
String remainder;
if (expr.startsWith("get[")) {
// get[N] syntax
int closeBracket = expr.indexOf(']');
if (closeBracket < 0) {
throw new RuntimeException("Invalid get expression, missing ]: " + expr);
}
index = Integer.parseInt(expr.substring(4, closeBracket));
remainder = expr.substring(closeBracket + 1).trim();
} else {
// get varname path
remainder = expr.substring(4).trim();
}
// Parse varname and path - could be "varname path" or "varname[...]"
String varName;
String path;
// Check if path starts with $ (explicit jsonpath)
if (remainder.startsWith("$")) {
// get $..path or get[0] $[*].foo - operates on 'response'
varName = "response";
path = remainder;
} else {View on GitHub (pinned to a22eb90246)