Json's response analysis returned to Gatling

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

My answer from the server:

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 do 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 }) 

He is still typing the whole answer. How can I just get the id "x8utwb2unq8uey23vpj64t65" ?

+7
json scala gatling
source share
1 answer

It is best to use a bit of jsonPath to pull out the id you need by storing this in your own variable for later use. Remember that jsonPath is still CheckBuilder, so you cannot just directly access the result - it may not match.

Converting it to Option[String] seems reasonable, though doing it.

So your last few lines will become:

  ... .check( jsonPath("$.id").saveAs("myresponseId") ) ) ).exec(session => { val maybeId = session.get("myresponseId").asOption[String] println(maybeId.getOrElse("COULD NOT FIND ID")) session }) 
+20
source share

All Articles