Dynamically selecting index from an array of responses

Hi; apologies if I’ve missed how to do this in the docs.

I have a json response from my first API call; and I would like to randomly select one of the objects to use for the rest of the test.

Perhaps there is a much simpler way to tackle this; but I’ve come up with

Calls the first API
Save all of the response to the session
Save the length of the response’s array to the session
Reads the length of the Array
This step is not compiling, but the goal would be: Reprocess the response through jmesPath and select an object by index.

The compile error is: [error] found : String [error] required: io.gatling.core.check.jmespath.JmesPaths

If this is a reasonable direction; how can I re-create JmesPaths?

.exec(http("datasets")
.get("/api/datasets")
.headers(headers_0("#{api_key}"))
.check(
status.gte(200),
status.lte(299),
jmesPath("[]").saveAs("all_may_datasets"),
jmesPath("[] | length(@)").saveAs("all_may_datasets_length")
))
.pause(1)
.exec(http("Open_dataset")
.get(session => {
val all_may_datasets_length = session("all_may_datasets_length").as[Int]
println(s"***all_may_datasets_length: ${all_may_datasets_length}")
jmesPath(s"[${all_may_datasets_length-1}]")(session("all_may_datasets").as[String])
s"/"
})
)

Thanks

I’d say that you have to:

Thanks for the pointers; I’ve tried updating the code as below

.exec(http("datasets")
.get("/api/datasets")
.headers(headers_0("#{api_key}"))
.check(
jmesPath("[].id").saveAs("all_datasets")
))
.pause(1)
.exec(http("Open_dataset")
.get(session => {
println(s" session.attributes: ${session.attributes}")
val random_dataset_id = session("#{all_datasets.random()}").as[Int]

This is erroring on: j.u.NoSuchElementException: No attribute named '#{all_datase…

From the print line of session.attributes; I can see
session.attributes: HashMap…, all_datasets → [140,215, …

So I think I’m using the wrong way to try and run Gatling Expression Language (EL) Strings against the session?

Thanks

Session#get takes an exact attribute key.
You can only use Gatling Expression Language when passing a String parameter to Gatling DSL methods.
You can’t use Gatling Expression Language nor Gatling DSL in custom code/functions.

Where do you want to pass a random entry? In a body, right?

Then, .body(StringBody("#{all_datasets.random()"))

Got it; thank you very much for the quick responses.