如何基于空手道中的过滤器从jsonarray中删除对象 [英] How to remove an object from an jsonarray based on filter in karate

查看:65
本文介绍了如何基于空手道中的过滤器从jsonarray中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于过滤器从json数组中删除一个对象,尝试在以下代码下尝试但不起作用

I want to remove an object from json array based on a filter, tried below code but didn't work

* def json = [ { "id": "0a7936ed", "code": "test", "label": "test", "type": "sell" }, { "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" } ]

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

I want remove object where code is equal to test

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

获取异常如下:

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)

请建议我如何过滤并删除对象. 谢谢..

Please suggest me how to filter it and remove the object. Thanks inadvance..

推荐答案

此用例正是karate.filter()设计的目的:

This use-case is exactly what karate.filter() is designed for:

* 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" }]

这里是文档: https://github.com/intuit/karate#空手道对象

这篇关于如何基于空手道中的过滤器从jsonarray中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆