1

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]

Shreyansh Jain
  • 498
  • 4
  • 7

1 Answers1

0

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
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248