Search code examples
jsonscalagatlingscala-gatling

How to convert a Gatling jsonFeeder with nested structures to json request body?


I have a json feeder reading from a file a json array like this:

[{ "a": {"b": 1} }, { "a": {"b": 2} }]

When I use it for a request body it's sent like ArraySeq(HashMap(.. instead of the actual json. How can I convert the Parsed Json back to Json keeping the feeder approach?

val jsonFileFeeder = jsonFile("requests.json").circular

val scn = scenario("cost estimation")
  .feed(jsonFileFeeder)
  .exec(
    http("request_1")
      .post("/")
      .body(StringBody(
        """{
    "a":  "${a}"
}"""
      )).asJson
  )


Solution

  • Use jsonStringify(), see documentation.

    val jsonFileFeeder = jsonFile("requests.json").circular
    
    val scn = scenario("cost estimation")
      .feed(jsonFileFeeder)
      .exec(
        http("request_1")
          .post("/")
          .body(StringBody(
            """{
        "a":  "${a.jsonStringify()}"
    }"""
          )).asJson
      )