我们可以选择使用空手道脚本过滤数据吗?

时间:2018-08-07 07:22:54

标签: karate

我有以下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数据。有什么选择吗

1 个答案:

答案 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' }