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

2,057 views
Skip to first unread message

Sameer Mainkar

unread,
Nov 28, 2014, 4:09:18 AM11/28/14
to gat...@googlegroups.com
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


Stéphane Landelle

unread,
Nov 28, 2014, 8:37:42 AM11/28/14
to gat...@googlegroups.com
You can simply add a transform step to do that. Use ThreadLocalRandom.

--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stéphane Landelle

unread,
Nov 28, 2014, 8:38:39 AM11/28/14
to gat...@googlegroups.com
Oups, stupid me: there's a random function in Gatling EL: http://gatling.io/docs/2.0.3/session/expression_el.html#expression-language

Sameer Mainkar

unread,
Nov 30, 2014, 11:37:31 PM11/30/14
to gat...@googlegroups.com
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 ने लिखा:

Kuldeep Linganna

unread,
Jun 24, 2020, 7:55:13 AM6/24/20
to Gatling User Group
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

Sameer Mainkar

unread,
Jun 24, 2020, 11:42:24 PM6/24/20
to Gatling User Group

Sameer Mainkar

unread,
Jun 24, 2020, 11:42:24 PM6/24/20
to Gatling User Group
Hi Kuldeep,

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

"${foo.random()}"

On Wednesday, June 24, 2020 at 5:25:13 PM UTC+5:30, Kuldeep Linganna wrote:

Kuldeep L

unread,
Jun 26, 2020, 1:42:31 AM6/26/20
to gat...@googlegroups.com
Thanks sameer ,

i resolved the issue with above method thanks for helping 

Kuldeep L

unread,
Jun 26, 2020, 7:06:36 AM6/26/20
to gat...@googlegroups.com
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

Kuldeep L

unread,
Jun 28, 2020, 2:53:02 PM6/28/20
to gat...@googlegroups.com
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 .
 

Sameer Mainkar

unread,
Jun 29, 2020, 2:31:56 AM6/29/20
to Gatling User Group
Hi Kuldeep,

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



Sameer
Reply all
Reply to author
Forward
0 new messages