extracting random element from json response and using it in next request

Hello,

I am trying to parse a json response and get all the instances of specific key. I want to randomly extract one of the value for that key in my next request. Currently I am getting Map and have to specifically select the value from that map. Can I randomize it.

My Code:

.check(jsonPath("$.entries[*].id").findAll.saveAs(“id”))) ----> This returns all “id” present under “entries”, like Map(id → Vector(id1, id2, id3, id4, id5,…)).

I would like to randomly pick up any of those id’s returned above in my request, instead I am currently specifying it like shown below:

.get("/nextRequest/${id(3)}")

Would it be possible to randomly assign id in above request instead of ${id(3)}.

Thanks in advance for all the help.

Sameer

You can simply add a transform step to do that. Use ThreadLocalRandom.
http://gatling.io/docs/2.0.3/http/http_check.html#transforming

Oups, stupid me: there’s a random function in Gatling EL: http://gatling.io/docs/2.0.3/session/expression_el.html#expression-language

Thanks Stephen, this is much simpler than transform step. I had used transform function specified in one of the earlier posts.

This helps a lot.

Thanks once again.

शुक्रवार, 28 नवम्बर 2014 7:08:39 pm UTC+5:30 को, Stéphane Landelle ने लिखा:

HI Sameer,

can you explain me how you did that…am in same situation where i need to pass the random values into next request from vector

Hi Kuldeep,

This was long back that I had did. Hope it holds true still,if I recollect correctly, had used below:

"${foo.random()}"

Thanks sameer ,

i resolved the issue with above method thanks for helping

And also i need to check can we exclude one value form vector and pass randomly to next request.

ex-vector(null,1,2,3,4,5) i need all these values to next call but not NULL how to achive this

Hi Team,

please help in below scenario.

i need to extract the ids from response in an array where 1 id having null value i need to ignore that “null” value and save other ids and pass to next request randomly.

sample response like below:-
[
{
“id”: null,

},
{

“id”: 1,

},
{

“id”: 2,

},
{

“id”: 3,

},
{

“id”: 4,

},
{

“id”: 5,

}
]

am trying below line to extract

jsonPath("$[*].id").ofType[Int].findAll.saveAs("allId")
still am getting null value in response to how to ignore **null** and pass other values randomly to next request please help me .
 

Hi Kuldeep,

Below is not question for Gatling User group. This is a typical scala/java question. You can refer to below links.

https://stackoverflow.com/questions/24512942/play-how-to-remove-the-fields-without-value-from-json-and-create-a-new-json-wit

https://gist.github.com/d6y/eda9d968e78943e672ce

Sameer