我有以下JSON响应:
{
"name": "Cabinets, Enclosures and Racks",
"parentId": "",
"categoryId": "55040",
"count": "3291",
"categoryLevel": "TOP"
},
{
"name": "Cable Management and Accessories",
"parentId": "55040",
"categoryId": "55041",
"count": "573",
"categoryLevel": "MIDDLE"
},
{
"name": "Cable Management",
"parentId": "55041",
"categoryId": "55042",
"count": "467",
"categoryLevel": "BOTTOM"
},
我需要获取唯一的TOP CategoryLevel数据。有什么选择吗
答案 0 :(得分:1)
是的,您为什么不更仔细地阅读文档:https://github.com/intuit/karate#jsonpath-filters
尝试将下面的内容粘贴到全新的Scenario
中,然后查看其是否有效:
* def response =
"""
[
{
"name":"Cabinets, Enclosures and Racks",
"parentId":"",
"categoryId":"55040",
"count":"3291",
"categoryLevel":"TOP"
},
{
"name":"Cable Management and Accessories",
"parentId":"55040",
"categoryId":"55041",
"count":"573",
"categoryLevel":"MIDDLE"
},
{
"name":"Cable Management",
"parentId":"55041",
"categoryId":"55042",
"count":"467",
"categoryLevel":"BOTTOM"
}
]
"""
* def top = $response[?(@.categoryLevel=='TOP')]
* match top[0] == { name: 'Cabinets, Enclosures and Racks', parentId: '', categoryId: '55040', count: '3291', categoryLevel: 'TOP' }