1
votes

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") 
        session.set("MY_RESULT_INFO", session("MY_RESULT_INFO").as[List[(String,Int)]] :+ Tuple2(objectId, version))
      })
    }
    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. What could cause this ?

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: Gatling - Looping through JSON array and it is not exactly answering my case.

Many Thanks!

1
The (1) line looks suspicious, it will return all substructures, probably not what you want. (2) might miss a string interpolator prefix, sorry I am not familiar with gatling.Gábor Bakos
(Just checked the link you mentioned. (2) is probably the correct syntax, just a bit unusual.)Gábor Bakos

1 Answers

0
votes

I found a way to do it with a regex.

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

I can then use this

foreach("${OBJECTS}", "object") {
  exec(
    http("Next API call")
      .get(commons.base_url_ws + "/my-resource/2.0/foo/${object._1}/${object._2}")
    [...]
}