如何根据空手道中的过滤器从jsonarray中删除对象

use*_*101 1 karate

我想根据过滤器从 json 数组中删除一个对象,尝试了下面的代码但没有用

* def json = [ { "id": "0a7936ed", "code": "test", "label": "test", "type": "sell" }, { "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" } ]
Run Code Online (Sandbox Code Playgroud)

我想删除代码等于测试的对象

* def fun = function(){ karate.remove('json', $.[?(@.code=='test')]") }
* call fun
Run Code Online (Sandbox Code Playgroud)

获取异常如下:

com.intuit.karate.exception.KarateException: javascript function call failed: String index out of range: -1
    at com.intuit.karate.Script.evalFunctionCall(Script.java:1622)
    at com.intuit.karate.Script.call(Script.java:1573)
    at com.intuit.karate.Script.callAndUpdateConfigAndAlsoVarsIfMapReturned(Script.java:1690)
    at com.intuit.karate.StepDefs.callAndUpdateConfigAndVars(StepDefs.java:582)
Run Code Online (Sandbox Code Playgroud)

请建议我如何过滤它并删除对象。提前致谢..

Pet*_*mas 6

这个用例正是karate.filter()为以下目的而设计的:

* def json = [ { "id": "0a7936ed", "code": "test", "label": "test", "type": "sell" }, { "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" } ]
* def condition = function(x){ return x.code != 'test' }
* def filtered = karate.filter(json, condition)
* match filtered == [{ "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" }]
Run Code Online (Sandbox Code Playgroud)

这是文档:https : //github.com/intuit/karate#the-karate-object