How to extract the data from the list of map in karate features?

I am querying a db and getting the below response and stored the db response in refData variable.
And def refData = here is my db query.
below is my json array response

[
  {
    "ref": 1
  },
  {
    "ref": 2
  },
  {
    "ref": 3
  },
  {
    "ref": 4
  },
  {
    "ref": 5
  }
]

I tried printing refData[0].ref then it prints 1
I tried these but it does not work.

refData.ref
refData[*].ref
refData[$].ref

how to fetch only the numbers. my expected output is [1,2,3,4,5]

>Solution :

Of late, I recommend using JS instead of JsonPath. Much easier and more readable:

* def refs = refData.map(x => x.ref)
* match refs == [1, 2, 3, 4, 5]

For more explanation: https://stackoverflow.com/a/76091034/143475

For completeness here is how you can do this using JsonPath. Maybe you missed the $. Please refer the docs: https://github.com/karatelabs/karate#jsonpath-filters

* def refs = $refData[*].ref

Leave a Reply