1

I am trying to dynamically substitute the value 'US' in the below expression. where in configs is an array not a json object.

* def selectedConfig= $configs[?(@.configId=='US')] 

#this above one works

But I am not successful making it a dynamic expression. I tried the below variant and fe, please help

* def activeConfigId = 'US'
* def selectedConfig= karate.jsonPath("$configs[?(@.configId=='" + activeConfigId + "')]")

I see there is someone else also asked similar question but no answer how to make this expression dynamic. Karate: parametric json path expressions

1 Answers1

1

You need to read the docs and examples a little more carefully. Here is an example that works:

* def configId = 'US'
* def response = [{configId: 'AA', data: 'first'}, {configId: 'US', data: 'second'}]
* def selectedConfig = karate.jsonPath(response, "$[?(@.configId=='" + configId + "')]")
* match selectedConfig[0] == { configId: 'US', data: 'second' }

If using karate.jsonPath() is too hard, please look at karate.filter(): https://stackoverflow.com/a/62897131/143475

And since you seem to be trying to do some smart config switching, refer this: https://stackoverflow.com/a/49693808/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248