英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论