使用空手道在 JsonPath 的过滤器部分中使用数组占位符进行表达式

Expression with Array placeholder in the filter part of JsonPath using karate

我正在尝试过滤响应,我想接受多个匹配项。发现 jsonPath 有“in”运算符来匹配左侧的值和右侧的数组元素。 字符串连接在 jsonPath 查询字符串中有效,但如何添加数组?

 * def my_array = ["a","b"]
 * def resp = karate.jsonPath(response, "$.content[?(@.smthing in my_array)]" )

作为解决方法,我可以使用正则表达式而不是数组。但这只是很多努力.... :(

* queryString = ""
* def func = function(x){queryString += x +"|";}
* karate.forEach(my_array,func)
* def trunc = function( ) { queryString = queryString.substring(0, queryString.length()-1) }
* trunc()
* def resp = karate.jsonPath(response, "$.content[?(@.smthing =~ /'+queryString +'/)]" )

对于像这样的复杂情况,我建议使用karate.filter()https://github.com/intuit/karate#json-transforms

* def myArray = ['a', 'b']
* def fun = function(x){ return myArray.includes(x) }
* def response = ['a', 'b', 'c', 'd']
* match karate.filter(response, fun) == ['a', 'b']