My response looks like this
{
"data":[
{
"id":"919274ee42fe01d40f89f51239009a2b",
"descriptor":"Long Count_copy_1938",
"alias":"longCount_Copy_1938",
"type":"Numeric"
},
{
"id":"435274ee42fe01d40f89f51239009a2b",
"descriptor":"Long Count2",
"alias":"longCount_Copy2",
"type":"Numeric"
},
{
"id":"345274ee42fe01d40f89f51239009a2b",
"descriptor":"Short Count2",
"alias":"Short count",
"type":"Numeric"
}
]
}
I would like to extract "descriptor":"id" to a Map. After mapping, the Map object should look like
"Long Count_copy_1938" -> "919274ee42fe01d40f89f51239009a2b"
"Long Count2" -> "435274ee42fe01d40f89f51239009a2b"
"Short Count2" -> "345274ee42fe01d40f89f51239009a2b"
Here is how I am achieving it, Let me know if there is a better way. Thanks!
exec(http("Get Field ids")
.get(s"${wqlDataSources}/")
.check(status.is(200),
jsonPath("$.data[*].descriptor").findAll.saveAs("descriptors"),
jsonPath("$.data[*].id").findAll.saveAs("ids")))
.exec(session => {
val descriptors = session("descriptors").as[Vector[String]]
val ids = session("ids").as[Vector[String]]
val fieldIdMap = (descriptors zip ids).toMap
session.set("fieldIdResultMap", fieldIdMap)
session.remove("descriptors").remove("ids")
})
Most JSONPath implementations support to extract multiple values in one go using the [,]
union operator, e.g. $..['id','descriptor']
to matches your two properties.
(However, the availability and the result of the union is neither universal nor consistent, if you check the above path online here by switching to Goessner or Jayway you will notice the result are not the same, the Gattling tab on the test site even throws an error; I cannot tell if it would work if the site would use the latest version.)
I could not find any documentation that confirmed Gatling supports unions but I found this test on Gatling's Github repo that has a union: JsonPath.query("$.menu['file','year']", json) ...
So, unioning should work, in general.
With some trial and error, I found this path that works using Gatling (even with older versions):
$.data[*]['id','descriptor']
Which returns:
[ "919274ee42fe01d40f89f51239009a2b", "Long Count_copy_1938", "435274ee42fe01d40f89f51239009a2b", "Long Count2", "345274ee42fe01d40f89f51239009a2b", "Short Count2" ]
So, you should be able to map the key/value pairs in one go using this path!