Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a variable in JsonPath filter

Tags:

karate

I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:

* def name = 'A Name'
* def ids = $response[?(@.name == '#(name)')].id

If I use the literal string 'A Name' in the RHS of the expression it works as expected.

I've tried various ways to get the variable to evaluate: '<name>', "#(name)", etc.

I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?

like image 413
kpschmidt Avatar asked Dec 07 '25 20:12

kpschmidt


1 Answers

Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions

And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters

* def ids = karate.jsonPath(response, "$.kittens[?(@.name=='" + name + "')].id")
like image 80
Peter Thomas Avatar answered Dec 09 '25 15:12

Peter Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!