如何从Karate功能中的地图列表中提取数据?

huangapple go评论52阅读模式
英文:

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]

英文:

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]

答案1

得分: 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
英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 18:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定