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

**URL:** <https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754>\
**Category:** Gatling (Open-Source)\
**Created:** [November 28, 2014, 9:09am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754 "2014-11-28T09:09:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sameer\_Mainkar](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sameer\_Mainkar](https://community.gatling.io/u/Sameer_Mainkar)\
**Post date:** [November 28, 2014, 9:09am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/1 "2014-11-28T09:09:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [November 28, 2014, 1:37pm UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/2 "2014-11-28T13:37:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [November 28, 2014, 1:38pm UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/3 "2014-11-28T13:38:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sameer\_Mainkar](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sameer\_Mainkar](https://community.gatling.io/u/Sameer_Mainkar)\
**Post date:** [December 1, 2014, 4:37am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/4 "2014-12-01T04:37:31Z")

</div>

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 ने लिखा:

---

<div class="post-metadata">

**Author:** ![Kuldeep\_Linganna](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@Kuldeep\_Linganna](https://community.gatling.io/u/Kuldeep_Linganna)\
**Post date:** [June 24, 2020, 11:13am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/5 "2020-06-24T11:13:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sameer\_Mainkar](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sameer\_Mainkar](https://community.gatling.io/u/Sameer_Mainkar)\
**Post date:** [June 25, 2020, 3:00am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/6 "2020-06-25T03:00:21Z")

</div>



---

<div class="post-metadata">

**Author:** ![Sameer\_Mainkar](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sameer\_Mainkar](https://community.gatling.io/u/Sameer_Mainkar)\
**Post date:** [June 25, 2020, 3:01am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/7 "2020-06-25T03:01:59Z")

</div>

Hi Kuldeep,

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

```auto
"${foo.random()}"

```

---

<div class="post-metadata">

**Author:** ![Kuldeep\_L](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kuldeep\_L](https://community.gatling.io/u/Kuldeep_L)\
**Post date:** [June 26, 2020, 5:31am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/8 "2020-06-26T05:31:26Z")

</div>

Thanks sameer ,

i resolved the issue with above method thanks for helping

---

<div class="post-metadata">

**Author:** ![Kuldeep\_L](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kuldeep\_L](https://community.gatling.io/u/Kuldeep_L)\
**Post date:** [June 26, 2020, 10:57am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/9 "2020-06-26T10:57:08Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Kuldeep\_L](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kuldeep\_L](https://community.gatling.io/u/Kuldeep_L)\
**Post date:** [June 28, 2020, 6:48pm UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/10 "2020-06-28T18:48:08Z")

</div>

> 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

```auto
jsonPath("$[*].id").ofType[Int].findAll.saveAs("allId")

```

```auto
still am getting null value in response to how to ignore **null** and pass other values randomly to next request please help me .

```

```auto
 

```

---

<div class="post-metadata">

**Author:** ![Sameer\_Mainkar](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sameer\_Mainkar](https://community.gatling.io/u/Sameer_Mainkar)\
**Post date:** [June 29, 2020, 3:58am UTC](https://community.gatling.io/t/extracting-random-element-from-json-response-and-using-it-in-next-request/1754/11 "2020-06-29T03:58:17Z")

</div>

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://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](https://gist.github.com/d6y/eda9d968e78943e672ce)

Sameer
