简体   繁体   English

如何使用空手道工具比较包含数组的 2 个 JSON 对象

[英]How to compare 2 JSON objects containing array using Karate tool

One of the API testing using intuit/karate,使用直觉/空手道的 API 测试之一,

Expected JSON is: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}预期的 JSON 是: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}

There is two possibility of an API response. API 响应有两种可能性。

First possible actual JSON: {name: hello, config:[{username: qwe, password: tyu},{username: abc, password: xyz}]}第一个可能的实际 JSON: {name: hello, config:[{username: qwe, password: tyu},{username: abc, password: xyz}]}

Second possible actual JSON: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}第二种可能的实际 JSON: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}

Likewise, the sequence of array element is different in actual response, hence following approach of validation of response throws error randomly.同样,数组元素的顺序在实际响应中是不同的,因此遵循响应验证的方法会随机抛出错误。

  • And response == < ExpectedResponse >和响应 == < 预期响应 >
  • And response contains < ExpectedResponse >并且响应包含 < ExpectedResponse >

Sometimes error is thrown as : Error : { Actual: response.config[0].abc, Expected: response.config[0].qwe }有时错误被抛出为:错误:{实际:response.config[0].abc,预期:response.config[0].qwe}

Sometimes error is thrown as : Error : { Actual: response.config[0].qwe, Expected: response.config[0].abc }有时会抛出错误:错误:{实际:response.config[0].qwe,预期:response.config[0].abc}

Would you please provide exact karate approach of JSON validation in which entire JSON along with ignore the sequence of element in JSON containing array ?您能否提供 JSON 验证的精确空手道方法,其中整个 JSON 以及忽略包含数组的 JSON 中的元素序列?

You need to read the docs carefully: https://github.com/intuit/karate#contains-short-cuts您需要仔细阅读文档: https : //github.com/intuit/karate#contains-short-cuts

* def response1 = {name: 'hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
* def response2 = {name: 'hello', config:[{username: 'abc', password: 'xyz'},{username: 'qwe', password: 'tyu'}]}

* def config = [{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]
* match response1 == { name: 'hello', config: '#(config)' }
* match response2 == { name: 'hello', config: '#(^^config)' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用空手道工具和特征文件比较包含数组的 2 个 JSON 对象 - How to compare 2 JSON objects that contains array using Karate tool and feature files 如何在空手道中将 Json Object 转换为 Json 数组? - How can I convert to Json Object to Json Array in Karate? 如何使用空手道验证非JSON响应主体 - How to validate Non-JSON response body using Karate 使用JavaScript,如何遍历2个JSON对象,比较它们的字段并根据比较创建一个新的JSON object? - Using JavaScript, how to traverse 2 JSON objects, compare their fields and create a new JSON object based on the comparison? 使用 Axios 来解析 JSON 对象,该对象只包含没有键的值 - Using Axios to parse JSON objects containing only values without keys 如何在 R 中使用 Plumber 使用一组 JSON 对象 - How Do I Consume an Array of JSON Objects using Plumber in R 如何使用对象数组读取 json - How to read json with an array of objects 在空手道中为数组响应断言和使用条件 - Asserting and using conditions for an array response in Karate 如何将给定的字典转换为包含Swift中的JSON数组的JSON对象? - How to convert the given dictionary into a JSON Object containing a JSON Array In Swift? 如果空手道中存在数组 object 属性,如何验证该属性 - How to validate Array object attribute if present in Karate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM