Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a Json response returned to Gatling

I am trying to parse a json response returned to gatling by the server.

My response from server is:

SessionAttribute(
  Session(
    GetServices,
    3491823964710285818-0,
    Map(
      gatling.http.cache.etagStore -> Map(https://api.xyz.com/services -> ), 
      gatling.http.cache.lastModifiedStore -> Map(https://api.xyz.com/services -> ),
      myresponse -> {
        "created":"2014-12-16T22:06:59.149+0000",
        "id":"x8utwb2unq8uey23vpj64t65",
        "name":"myservice",
        "updated":"2014-12-16T22:06:59.149+0000",
        "version":null
      }),
    1418767654142,622,
    OK,List(),<function1>),id)

I am doing this in my script:

val scn = scenario("GetServices")
          .exec(http("Get all Services")
          .post("/services")
          .body(StringBody("""{ "name": "myservice" }""")).asJSON
          .headers(sentHeaders)
          .check(jsonPath("$")
          .saveAs("myresponse"))
).exec(session => {
  println(session.get("id"))
  session
})

It is still printing the whole response. How can I just retrieve the id which is "x8utwb2unq8uey23vpj64t65"?

like image 389
user1075958 Avatar asked Dec 16 '14 22:12

user1075958


1 Answers

It might be easiest to use a little bit more jsonPath to pull out the id you need, storing that in its own variable for later use. Remember that jsonPath is still a CheckBuilder, so you can't just access the result directly - it might have failed to match.

Converting it to an Option[String] seems like a reasonable thing to do though.

So your final few lines would become:

    ...
    .check(
      jsonPath("$.id").saveAs("myresponseId")
    )
  )
).exec(session => {
  val maybeId = session.get("myresponseId").asOption[String]
  println(maybeId.getOrElse("COULD NOT FIND ID"))
  session
})
like image 106
millhouse Avatar answered Oct 07 '22 03:10

millhouse