Looping through JSON array and extract attributes

3,007 views
Skip to first unread message

Neil

unread,
Aug 13, 2014, 11:03:02 AM8/13/14
to gat...@googlegroups.com
I have a block of code which needs to loop through a JSON array which is obtained from response of a REST service.

.exec(http("Request_1")
  .post("/endPoint")
  .headers(headers_1)
  .body(StringBody("""REQUEST_BODY""")).asJSON
  .check(jsonPath("$.result").is("SUCCESS"))
  .check(jsonPath("$.data[*]").findAll.saveAs("pList")))
.exec(session => {
  println(session)
  session
})
.foreach("${pList}", "player"){
 exec(session => {
    val playerId = JsonPath.query("$.playerId", "${player}")
    session.set("playerId", playerId)
  })
 .exec(http("Request_1")
    .post("/endPoint")
    .headers(headers_1)
    .body(StringBody("""{"playerId":"${playerId}"}""")).asJSON
    .check(jsonPath("$.result").is("SUCCESS")))

}

The response format of the first request was

{
  "result": "SUCCESS",
  "data": [
    {
      "playerId": 2
    },
    {
      "playerId": 3
    },
    {
      "playerId": 4
    }
  ]
}

I am seeing in the second request the body is {"playerId":"Right(empty iterator)} Expected : 3 requests with body as {"playerId":1} {"playerId":2} {"playerId":3}

SOF Crosspost

Pierre DAL-PRA

unread,
Aug 14, 2014, 7:36:37 AM8/14/14
to gat...@googlegroups.com
Hi Neil, 

Couldn't you just extract playerIds directly from the first jsonPath query instead of doing it manually through a foreach block ? 
e.g. $.data[*].playerId  then a foreach on "pList".

Neil Ghosh

unread,
Aug 14, 2014, 7:43:54 AM8/14/14
to gat...@googlegroups.com
Hi Pierre,

That is what I eventually did . The actual response of my first request is having more than one attribute that I have to loop through

{
  "result": "SUCCESS",
  "data": [
    {
      "playerId": 2,
      "score" :100
    },
    {
      "playerId": 3,
      "score" :200
    },
    {
      "playerId": 4
      "score" :200"
    }
  ]
}

This is why I wanted to have the whole array in session and iterate later.
What I did as workarround is just relied on the relative ordering two lists one with IDs and other with Scores.
And fetched the corresponding score for an ID using the counter as Index (You may have seen my other question regarding saving list in session)

--
You received this message because you are subscribed to a topic in the Google Groups "Gatling User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gatling/4IRDSWzglSU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks and Regards
Neil
http://neilghosh.com


Benny Sadeh

unread,
Aug 15, 2014, 12:54:56 AM8/15/14
to gat...@googlegroups.com, neil....@gmail.com
i've used another json parser to do something similar (lift-json in my case):

    .exec(http("...")
      .get("...")
      .headers(headers_...)
      .check(bodyString.transform(extractXXX(_)).saveAs("xxx")))

  private def extractXXX(json: String) =
    (JsonParser.parse(json) \\ "xxx").values.asInstanceOf[Seq[Map[String, Any]]]

so later you can access ${xxx.whateverFieldName} 

Neil Ghosh

unread,
Aug 19, 2014, 3:39:22 PM8/19/14
to Benny Sadeh, gat...@googlegroups.com
Just so that everyone knows this has been answered in SOF by Michelle
http://stackoverflow.com/a/25384387/747456

Deepak Shakya

unread,
Mar 21, 2017, 7:41:19 AM3/21/17
to Gatling User Group, benny...@gmail.com, neil....@gmail.com
I have a similar setup wherein I am saving the multiple players. However, in case of empty result list, the request is marked as KO.

For example, if the output is
{
 
"result": "SUCCESS",
 
"data": []
}

In this case,  request is marked as KO below
Request:
player
-request: KO jsonPath($.value[*]).findAll.exists, found nothing

I understand this is because jsonpath() is inside check(). I was seeing the dontValidate in some other thread, but it doesn't exist in current implementation.

What should I do in this case?
Reply all
Reply to author
Forward
0 new messages