Gatling: JsonPath extract multiple values

I’m building a gatling 2.1.3 scenario and I need to extract data from a json body.

Example of the body:

[
{
“objectId”: “FirstFoo”,
“pvrId”: “413”
“type”: “foo”,
“name”: “the first name”,
“fooabilities”: {
“foo1”: true,
“foo2”: true
},
“versions”: [23, 23, 23, 23, 23, 23, 24, 23, 23],
“logo”: [
{
“type”: “firstlogo”,
“width”: 780,
“height”: 490,
“url”: “firstlogos/HD/{resolution}.png”
}
]
},
{
“objectId”: “SecondFoo”,
“pvrId”: “414”
“type”: “foo”,
“name”: “the second name”,
“fooabilities”: {
“foo1”: true,
“foo2”: false
},
“versions”: [23, 23, 23, 23, 23, 23, 24, 23, 23],
“logo”: [
{
“type”: “secondlogo”,
“width”: 780,
“height”: 490,
“url”: “secondlogos/HD/{resolution}.png”
}
]
}
]

and I have this code trying to extract de data:

exec(
http(“get object”)
.get(commons.base_url_ws + “/my-resource/2.0/object/”)
.headers(commons.headers_ws_session).asJSON
.check(jsonPath("$…*").findAll.saveAs(“MY_RESULT”))) (1)
.exec(session => {
foreach("${MY_RESULT}", “result”) { (2)
exec(session => {
val result= session(“result”).as[Map[String, Any]]
val objectId = result(“objectId”)
val version = result(“version”) (3)
session.set(“MY_RESULT_INFO”, session(“MY_RESULT_INFO”).as[List[(String,Int)]] :+ Tuple2(objectId, version)) (4)
})
}
session
})

My goal is:
To extract the objectId and the 9th value from the version array.
I want it to look as Vector → [(id1, version1),(id2, version2)] in the session to reuse later in another call to the API.

My concerns are:
(1) Is this going to create entries in the session with the complete sub objects, because in other answers I was that is was always a map that was saved (“id” = [{…}]) and here I do not have ids.

(2) In the logs, I see that the session is loaded with a lot of data, but this foreach is never called.

(3) This is an array in the json but I can’t find how to extract data from it at the moment.

(4) I try to add the data to the already existing one since it is a foreach.

My experience in Scala is of a beginner so please point out if I have clear issues I did not see.

I have looked into this issue: http://stackoverflow.com/questions/25289334/gatling-looping-through-json-array and it is not exactly answering my case.

Many Thanks!

How about creating two checks

`
.check(jsonPath("""$…objectId""").findAll.saveAs(“objectids”))

.check(jsonPath("""$..versions[8]""").findAll.saveAs("versions")) //only the 9th element will be saved here.

`

Then in foreach, use whichever variable you want to.

I thought about this option but I need to use the id and the version as a tuple per jsonBlock. So the second id must be linked to the second version. So a foreach would be hard to implement since I don’t have an index on the current value.

I used a regex instead and got to this solution:

.check(regex("""(?:“objectId”|“version”):"(.?)",.?(?:“objectId”|“version”):[(?:.?,){9}([0-9]?),.*?]""").ofType[(String, String)].findAll saveAs (“OBJECTS”)))

It’s working fine but I would have enjoyed to stick with jsonPath for extracting this information.

thanks for you reply!

I thought about this option but I need to use the id and the version as a

tuple per jsonBlock. So the second id must be linked to the second version.
So a foreach would be hard to implement since I don't have an index on the
current value.

Do a repeat <http://gatling.io/docs/2.1.4/general/scenario.html#repeat&gt;
loop of the size
<http://gatling.io/docs/2.1.4/session/expression_el.html#expression-language&gt;
of the objectids, where you explicitly set the name of the loop index.
Then use this index
<https://github.com/gatling/gatling/blob/master/gatling-core/src/test/scala/io/gatling/core/session/el/ElSpec.scala#L158&gt;
to fetch the proper element from each collection.

.exec(session => {
        foreach

No way this would work. You can't use DSL elements instead those functions.

*Stéphane Landelle*
*Lead developer*
slandelle@gatling.io

Awesome, you just fixed my problem!
Merci Stéphane!

De rien!