Karate DSL: How to Fix Variables Not Evaluating in JSON Requests
- 23 Jun, 2026
While building a request body in Karate DSL, I ran into a small but confusing issue.
I had a value returned from a Java helper:
* def bookingId = result.param
Then I used it to build a JSON request:
* def myRequest = { "booking_id": bookingId }
I expected:
{
"booking_id": 123456
}
But Karate was creating:
{
"booking_id": "bookingId"
}
Why It Happens
When Karate parses an inline JSON object, values are not always evaluated as JavaScript expressions. In this case, bookingId was treated as literal text instead of the variable’s value.
The Fix
Wrap the JSON object in parentheses:
* def myRequest = ({ "booking_id": bookingId })
Now Karate evaluates the expression correctly and produces:
{
"booking_id": 123456
}
What’s Special About the Parentheses?
The parentheses tell Karate to evaluate the entire object as a JavaScript expression. Once the object is evaluated by the JavaScript engine, variables such as bookingId in this example, are resolved to their actual values instead of being treated as strings.
That’s it. If you ever see Karate sending a variable name instead of its value, check whether your JSON object needs to be wrapped in parentheses.