Get all values from Single key Multiple values Json response and store in an array

Hi All,

I am facing the below scenario and any solution would be really helpful.

  1. Need to get all values from a Single key multiple value Json response like the below one.
    {“num”: 1, “templates”: [2285, 2293, 2310, 6638]}. I am able to successfully get it

using jsonPath("$…templates[0]") and Regex regex("\[(.*?)\]"). The total comma separated values of templates key changes dynamically.

  1. But the problem is the resultant value when I took from the session and print in console it looks like this [2285, 2293, 2310, 6638], 2285, 2293, 2310, 6638 for Jsonpath and regex respectively.

  2. I tried to use the Scala Split and tried to push it into an array but it is not working.

  3. I need to get the value into an array and iterate the below request , depending on the total count with

.exec(http(“A”)
.get(url + “/${templates}/files/”)
.headers(templates)
.check(status.is(200))

Regards,
Aravind

Hi All,

After a grinding trail and error method and lot of googling and repetitive runs. I am able to achieve the above scenario in Gatling.
The high level solution is given below,

  1. I stored the session value of templates as given below
    val laa: Vector[String] = session(" templates ").as[Vector[String]]

  2. Then got the count of the vector array using the below
    val count1 = laa.size

  3. Define i val and set it in a session

  4. Check statement between i val and the array size to repeat the below code block for the times of array size

dowhile(session => (session(“i”).as[String].toInt < session(“count1”).as[String].toInt)(

.exec(http(“A”)
.get("/B/C/${laa(i)}/D/") // For each iteration all the values in the array will be executed one by one
.headers(F)
.check(status.is(200))))

// incrementing i

.exec(
session => {
i += 1
println("i is ")
println(i)
session.set(“i”, i)
}

)

Regards,
Aravind G

It looks like you’ve reimplemented foreach: https://gatling.io/docs/current/general/scenario/#foreach