提问人:Chethan Swaroop 提问时间:8/30/2021 更新时间:8/30/2021 访问量:297
当响应是随机顺序的 JSON 对象数组时,如何在 KarateAPI 中验证响应对象 [duplicate]
How to validate response object in KarateAPI when the response is an array of JSON objects in random order [duplicate]
问:
我有以下答复:
{
"success":[
{
"id":"123",
"value" :"abc"
},
{
"id":"456",
"value" :"xyz"
}
}
我想验证我的响应中 id “123” 的值是否为“abc”。
如果保证我的响应中 JSON 对象的顺序,我会使用以下方法进行评估:
* match respone.success[0].value == "abc"
但我在响应中对象的顺序是未知的。如何检查 id 为“123”的值是否为“abc”
答:
1赞
Peter Thomas
8/30/2021
#1
给你。请参阅已经解释过所有内容的文档:
* def found = $response.success[?(@.id=='123')]
* match found[0] contains { value: 'abc' }
提示,您可以重构 JSON 以使其更易于管理:
* def fun = function(x){ var key = x.id; var res = {}; res[key] = x.value; return res }
* def data = karate.map(response.success, fun)
* match data == [{ '123': 'abc' }, { '456': 'xyz' }]
同样,请参考文档和其他答案: https://github.com/intuit/karate#json-transforms
评论