英文:
How to get all the values from a key using Karate
问题
A Karate beginner here
This is my Scenario Outline that I am trying to use
Scenario Outline: API Should Support Sorting
Given path 'mypathHere'
And params {sortColumn: <sort>, isActive:1}
When method Get
Then status 200
* def data = karate.jsonPath(response,"$..<sortColumn>")
* def dataSorted = karate.sort(data)
And match data == dataSorted
Examples:
| sort | sortColumn |
| name | name |
| unitCode | code |
| shortName | shortName |
I have a response with an object that looks like this
[
{
"type": "unit",
"id": 1004,
"name": "Name 1",
"code": "Code Name 1",
"shortName": "Short Name 1"
},
{
"type": "unit",
"id": 615,
"name": "Name 2",
"code": "Code 2",
"shortName": "Short Name 2"
},
{
"type": "unit",
"id": 1071,
"name": "Name 3",
"code": "Code 3",
"shortName": "Short Name 3"
}
]
I need to get a list of all the values for one of the keys.
I know that i can use this approach
- def data = karate.jsonPath(response,"$..name")
But I wonder if there's a **Karate way** that I can use?
英文:
A Karate beginner here
This is my Scenario Outline that I am trying to use
Scenario Outline: API Should Support Sorting
Given path 'mypathHere'
And params {sortColumn: <sort>, isActive:1}
When method Get
Then status 200
* def data = karate.jsonPath(response,"$..<sortColumn>")
* def dataSorted = karate.sort(data)
And match data == dataSorted
Examples:
| sort | sortColumn |
| name | name |
| unitCode | code |
| shortName | shortName |
I have a response with an object that looks like this
[
{
"type": "unit",
"id": 1004,
"name": "Name 1",
"code": "Code Name 1",
"shortName": "Short Name 1"
},
{
"type": "unit",
"id": 615,
"name": "Name 2",
"code": "Code 2",
"shortName": "Short Name 2"
},
{
"type": "unit",
"id": 1071,
"name": "Name 3",
"code": "Code 3",
"shortName": "Short Name 3"
}
]
I need to get a list of all the values for one of the keys.
I know that i can use this approach
* def data = karate.jsonPath(response,"$..name")
But I wonder if there's a Karate way that I can use?
答案1
得分: 2
# 第一种解决方案
* def data = response.map((item) => item.<sortColumn>)
* 打印 data
# 第二种解决方案
* def data2 = $response[*].<sortColumn>
* 打印 data2
示例:
| sort | sortColumn |
| name | name |
| unitCode | code |
| shortName | shortName |
英文:
First solution
* def data = response.map((item) => item.<sortColumn>)
* print data
second solution
* def data2 = $response[*].<sortColumn>
* print data2
Examples:
| sort | sortColumn |
| name | name |
| unitCode | code |
| shortName | shortName |
答案2
得分: 2
我会争辩说,你正在按照"karate"的方式来做,因为你在示例中实际上使用了"karate"关键词来获取期望的值。你的方法没有问题。以下是另一种使用karate.map的方法:
- 定义键 =
- 定义值 = karate.map(response,函数(item) { return item[键] })
英文:
I'd argue you are doing it the "karate" way as you are literally using the karate key word in your example to get the expected values. There is nothing wrong with your approach. Here is another way to go about it using karate.map
* def key = <sortColumn>
* def values = karate.map(response, function(item) { return item[key] })
答案3
得分: 1
这是一个有趣的挑战。以下是我的解决方案:
* def result = {}
* def keys = ['id', 'name', 'code', 'shortName']
* keys.forEach(x => result[x] = response.map(y => y[x]))
* print result
它将给你一个包含了键和对应数值的单一对象。如果需要,你可以轻松地进行排序的组合。
英文:
That's a fun challenge. Here's my solution:
* def result = {}
* def keys = ['id', 'name', 'code', 'shortName']
* keys.forEach(x => result[x] = response.map(y => y[x]))
* print result
It will give you a single object, with the keys and values combined. You should be able to easily combine sorting if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论