英文:
Match SUM or concatenation of multiple elements of same JSON array using Karate
问题
以下是翻译好的部分:
拥有一个包含多个套装中许多乐器的JSON响应(>100+)。如何最佳匹配每个项目集的item_list
值与item_number
和item_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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论