Can we have option to filter the data using Karate scripts?

ε祈祈猫儿з 提交于 2021-01-28 06:11:07

问题


I have below JSON 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"
    },

I need to fetch the only TOP CategoryLevel data. Is there any option to do so


回答1:


Yes, why don't you read the docs a little more carefully: https://github.com/intuit/karate#jsonpath-filters

Try pasting the below in a fresh Scenario and see it working:

* 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' }


来源:https://stackoverflow.com/questions/51721086/can-we-have-option-to-filter-the-data-using-karate-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!