Manoj Kumar Kotturi
Manoj Kumar Kotturi

Reputation: 109

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

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

Upvotes: 1

Views: 1139

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

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

Upvotes: 1

Related Questions