匹配同一JSON数组的SUM或多个元素的连接使用Karate。

huangapple go评论103阅读模式
英文:

Match SUM or concatenation of multiple elements of same JSON array using Karate

问题

以下是翻译好的部分:

拥有一个包含多个套装中许多乐器的JSON响应(>100+)。如何最佳匹配每个项目集的item_list值与item_numberitem_info的精确总和或连接。

在以下总和或连接处验证失败:

* match each response[*].item_list = $response[*].item_number + "--" + $response[*].item_info

[{
   "item_number":01,
   "item_info": "Piano",
   "item_list": "01--Piano"
 },
 {
   "item_number":02,
   "item_info": "Mrudanga",
   "item_list": "02--Mrudanga"
 },
 {
   "item_number":03,
   "item_info": "Guitar",
   "item_list": "03--Guitar"
 },
 {
   "item_number":04,
   "item_info": "Flute",
   "item_list": "04--Flute"
 },
 ...
 ...
 ...
 ...
 ...
]
英文:

Having a JSON Response with so many musical instruments in multiple sets (>100+). How to Best match each of the item set item_list value with exact Sum or concatenation of item_number and item_info.

> Validation failing at below Sum or concatenation:

> * match each response[*].item_list = $response[*].item_number + "--" + $response[*].item_info

[{
   "item_number":01,
   "item_info": "Piano",
   "item_list": "01--Piano"
 },
 {
   "item_number":02,
   "item_info": "Mrudanga",
   "item_list": "02--Mrudanga"
 },
 {
   "item_number":03,
   "item_info": "Guitar",
   "item_list": "03--Guitar"
 },
 {
   "item_number":04,
   "item_info": "Flute",
   "item_list": "04--Flute"
 },
 ...
 ...
 ...
 ...
 ...
]

答案1

得分: 1

这将有效 如果数字值正确:

* 匹配每个响应包含 { item_list: "#(_$.item_number + '--' + _$.item_info)" }

您可能需要进行一些预处理:

* 定义数据 = response.map(x => String(x.item_number).padStart(2, '0') + '--' + x.item_info)
* 匹配 response[*].item_list == 数据
英文:

This would work if the number values were proper:

* match each response contains { item_list: "#(_$.item_number + '--' + _$.item_info)" }

You may need to do some pre-processing:

* def data = response.map(x => String(x.item_number).padStart(2, '0') + '--' + x.item_info)
* match response[*].item_list == data

huangapple
  • 本文由 发表于 2023年8月4日 02:30:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定