0

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.

Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69
  • 1
    either parse it by hand or use a JSON library: http://manuel.bernhardt.io/2015/11/06/a-quick-tour-of-json-libraries-in-scala/ – maasg Nov 11 '16 at 13:15

2 Answers2

2

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.

radumanolescu
  • 4,059
  • 2
  • 31
  • 44
  • This is not answering my question. I have to execute request 1 to get the Json parse the Json and make multiple requests depending on the count of elements in the request 1 response – Jordan Borisov Nov 11 '16 at 15:01
  • Sorry - not sure I understand which parts you know how to do and which not. For instance: "execute request 1 to get the Json" - is that the problem? "parse the Json" - does the above solution work? "make multiple requests depending on the count of elements in the request 1 response" - this is something you have not mentioned in the original question. After you parse the JSON, you have a list containing all the elements of the "request 1 response", no? Please clarify question. – radumanolescu Nov 11 '16 at 16:09
1

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")
  )
}
FlavienBert
  • 1,674
  • 3
  • 16
  • 17