malutki5200
malutki5200

Reputation: 1112

How to iterate over json response array

I have a question about Gatling.
I need to get the following response:

[
{
"id": 1,
"name": "Jack"
},
{
"id": 2,
"name": "John"
} 
]

grab those ids, iterate over them and make a new request for each of them. So far I have this:

.exec(
            http("Image list")
                .get("/api/img")
                .headers(headers_0)
                .check(
                    jsonPath("$..id").findAll.saveAs("imgs")
                )
        )

It successfuly saves ids to "imgs" which is session variable but I am not able to iterate over them or process it at all.

How can I process it? I am new to Gatling and Scala so I have no idea how to approach this.
Please help.

Upvotes: 0

Views: 1418

Answers (2)

Jeffrey Chung
Jeffrey Chung

Reputation: 19517

You can treat the imgs session variable as a Scala List:

val ids = session("imgs").as[List[Int]]
ids.foreach(id => ...)

An update to reflect the fact that the internal implementation is now a Vector, as OP has discovered:

val ids = session("imgs").as[Seq[Int]]

Upvotes: 1

malutki5200
malutki5200

Reputation: 1112

I found a solution. The only possible format is Seq. In my case this solves the problem:

val imageIds = session("imgs").as[Seq[String]]

Upvotes: 0

Related Questions