验证 json 列表中的每个项目都包含在另一个 json 列表中

Validate each item in a json list is contained in another json list

* def list1 = [{'a':'value'}, {'b':'value2'}, {'c':'value4'}]
* def list2 = [{'a':'value'}, {'b':'value2'}]

我想使用空手道验证 list2 中的所有项目是否都存在于 list1 中 我得到两个列表作为来自不同 API 的响应。给定示例中的一些子值包含 json 个对象。

我试图通过创建一个函数并维护一个验证成功数组(不是最优的)来解决这个问题。但这也失败了,因为 json 被比较为字符串和子 json 上例中提到的值中的对象具有不同的结构(有些包含括号和换行符之间的空格)。

def result = []
def iterateJson = 
'''
function(item){
if(karate.get('payload').contains(item)
   karate.append(result, true)
else
   karate.append(result, false)
}

'''
karate.foreach(response, iterateJson)
match each result[*] == true

你好像是over-thinking这个!

* def list1 = [{'a':'value'}, {'b':'value2'}, {'c':'value4'}]
* def list2 = [{'a':'value'}, {'b':'value2'}]
* match list1 contains list2

是的,处理了子对象。试试看。