Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading