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!