3

I have a JSON file which I am reading in Karate feature file which consists of IDs, something like below temp.json

{
  112354,
  221344,
  126789,
  122333
}

In my Karate feature file, I am calling an API that returns all the IDs available in the DB. The response of this is complex json arrays. To retrieve one ID from the response I have to do something like below

company[0].department[0].project[0].id

response json structure
{
  "company": [
    {
      "department": [
         {
           "project": [
             {
               "id": 567436
             },
             {
               "id": 123456
             }
            ]
         }
       ]
    }
   ]
}


now I want to match if all the IDs in temp.json are available in the json response I got from Karate by calling the API. It has to traverse through all the arrays to see if the IDs are available. Is there any way to accomplish this in Karate ?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39

1 Answers1

2

Quite likely you can achieve what you want in one line:

* def ids = [ 123, 234, 345 ]
* def response = [{ id: 123 }, { id: 234 }, { id: 345 }]
* match response..id == ids

So note how you can use .. in Json Path.

Please look at these other answers for more information:

https://stackoverflow.com/a/65036047/143475

https://stackoverflow.com/a/68363524/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248