Gatling: JsonPath extract multiple values

4,993 views
Skip to first unread message

Bruno Gagnon-Adam

unread,
Jan 28, 2015, 10:24:19 AM1/28/15
to gat...@googlegroups.com
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!

Abhinav Gogna

unread,
Jan 28, 2015, 11:20:57 AM1/28/15
to gat...@googlegroups.com
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.

Bruno Gagnon-Adam

unread,
Jan 28, 2015, 2:14:53 PM1/28/15
to gat...@googlegroups.com
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!

Stéphane LANDELLE

unread,
Jan 28, 2015, 2:49:42 PM1/28/15
to gat...@googlegroups.com
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 loop of the size of the objectids, where you explicitly set the name of the loop index.
Then use this index 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

Bruno Gagnon-Adam

unread,
Jan 28, 2015, 3:01:50 PM1/28/15
to gat...@googlegroups.com
Awesome, you just fixed my problem!
Merci Stéphane!

slan...@gatling.io

unread,
Jan 28, 2015, 3:27:28 PM1/28/15
to gat...@googlegroups.com
De rien!


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages