Yordan Borisov
Yordan Borisov

Reputation: 1652

Gatling convert Json array to Map

I have json such as ["123","123a","12c3","1f23","e123","r123"] as response from rest server.

I want to parse this json as Collection and iterate over it and make exec request over each element in it such as :

SERVER + "/get?param=${el}" where el will be 123,123a,12c3,1f23,e123 and r123

My question is how can I do it.

Upvotes: 0

Views: 2419

Answers (2)

FlavienBert
FlavienBert

Reputation: 1684

You should try something like this:

exec(
  http("my request")
    .get("/myrequest")
    .check(status.is(200))
    .check(jsonPath("$").ofType[Seq[String]].saveAs("params"))
).foreach("${params}", "param") {
  exec(http("request with parameter ${param}")
    .get("/get")
    .queryParam("param", "$param")
  )
}

Upvotes: 1

radumanolescu
radumanolescu

Reputation: 4161

You can do something like this:

import org.json4s._
import org.json4s.jackson.JsonMethods._
object JSonToMap {
  def main(args: Array[String]) {
    implicit val fmt = org.json4s.DefaultFormats
    val json = parse("""{ "response" : ["123","123a","12c3","1f23","e123","r123"] }""")
    val jsonResp = json.extract[JsonResp]
    println(jsonResp)
    jsonResp.response.foreach { param => 
      println(s"SERVER /get?param=${param}")
    }

  }
  case class JsonResp(response: Seq[String], somethingElse: Option[String])
}

Now you have a case class where the "response" member is a list of your strings. You can then manipulate this list however you need to create the requests to SERVER.

Upvotes: 2

Related Questions