not able to save session variables in csv file

Hello Team,

I have a JSON response with multiple values [say, IDs] in it, I want to extract all of them from the JSON response and save it in the .csv/feeder so that i can use them in th enest scenarios.

Json Response:

[
{
“id”: 5156,
“orderId”: 1,
“step”: “Step Desc”,
“result”: “Result Desc”
},

{
“id”: 5157,

“orderId”: 2,
“step”: “Step Desc”,
“result”: “Result Desc”

},
{
“id”: 5158,

“orderId”: 3,
“step”: “Step Desc”,
“result”: “Result Desc”

}
]

I want to save the highlighted values in the csv file and my feeder should look like
id
5156,5157,5158

Till now i have used below methods
.check(jsonPath("$…id").findAll.saveAs(“ID”)

.exec( session =>
{scala.tools.nsc.io.File("\ID.csv").appendAll(“ID”+",")
session}
)

However only first value is getting saved in the file and rest all getting ignored.
Kindly help to sort this issue.

Thanks in advance…!!

Hello Team,
Waiting for your response.

Your help would be much appreciated.

Thanks in advance…!!

Kind Regards,
Siddhi

findAll will save the values in an array. So you will need to loop over ID[i] and write that to the file in the session.

See example here → http://gatling.io/docs/2.2.2/http/http_check.html?highlight=saveas#http-response-body

Hello Team,

I tried using foreach, mkstring etc functions to extract the list of ID’s from the session however only first ID is getting saved and the findAll it not able to extract the next ID in Session body.

My result is getting printed as
viz 5156

There are no IDs which are getting fetched by findAll.

Hi Siddhi,

you can try something like this:

`
val scn = scenario(“Siddhi”)
.exec(http(“GET”)
.get("/my_way_to_your_JSON")
.check(jsonPath("$…id").findAll.saveAs(“IdList”))
)

.foreach("${IdList}", “id”) {
exec(session => {
println(session(“id”).as[String])
session})
}

`
In my example i print the iDs, you should change print to write to file

all IDs can be extracted by using below JSONPath expression
.check(jsonPath("$…[:].id").findAll.saveAs(“IdList”))

After this save all the IDs in Vector and write that vector in csv file/feeder.

`
val writer = {
val fos = new java.io.FileOutputStream(“foo.txt”)
new java.io.PrintWriter(fos,true)
}

val scn = scenario(“FK”)
.exec(http(“Get”)

.get("/my_path")
.check(jsonPath("$…id").findAll.saveAs(“IdList”))
)

.foreach("${IdList}", “id”) {
exec(session => {
writer.println(session(“id”).as[String])
session})
}
`