且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当响应是随机顺序的 JSON 对象数组时,如何在 KarateAPI 中验证响应对象

更新时间:2023-10-01 20:23:58

给你.请参阅已经解释了所有内容的文档:

Here you go. Please refer to the docs where everything is already explained:

* def found = $response.success[?(@.id=='123')]
* match found[0] contains { value: 'abc' }

提示,您可以重构 JSON 以使其更易于管理:

A tip, you can refactor the JSON to be easier to manage:

* 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/直觉/空手道#json-transforms

Again, please refer to the docs and other answers: https://github.com/intuit/karate#json-transforms