# Get all values from Single key Multiple values Json response and store in an array

**URL:** <https://community.gatling.io/t/get-all-values-from-single-key-multiple-values-json-response-and-store-in-an-array/6094>\
**Category:** Gatling (Open-Source)\
**Created:** [May 14, 2021, 10:28am UTC](https://community.gatling.io/t/get-all-values-from-single-key-multiple-values-json-response-and-store-in-an-array/6094 "2021-05-14T10:28:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aravind\_G](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Aravind\_G](https://community.gatling.io/u/Aravind_G)\
**Post date:** [May 14, 2021, 10:28am UTC](https://community.gatling.io/t/get-all-values-from-single-key-multiple-values-json-response-and-store-in-an-array/6094/1 "2021-05-14T10:28:26Z")

</div>

Hi All,

I am facing the below scenario and any solution would be really helpful.

1. Need to get all values from a Single key multiple value Json response like the below one.  
{“num”: 1, “templates”: [2285, 2293, 2310, 6638]}. I am able to successfully get it

using **_jsonPath("$…templates[0]") and Regex regex("\[(.\*?)\]"). The total comma separated values of templates key changes dynamically._**

1. But the problem is the resultant value when I took from the session and print in console it looks like this [2285, 2293, 2310, 6638], 2285, 2293, 2310, 6638 for Jsonpath and regex respectively.

2. I tried to **_use the Scala Split_** and tried to push it into an array but it is not working.

3. I need to get the value into an array and iterate the below request , depending on the total count with

.exec(http(“A”)  
.get(url + “/${ **_templates_** }/files/”)  
.headers(templates)  
.check(status.is(200))

Regards,  
Aravind

---

<div class="post-metadata">

**Author:** ![Aravind\_G](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Aravind\_G](https://community.gatling.io/u/Aravind_G)\
**Post date:** [May 15, 2021, 7:02am UTC](https://community.gatling.io/t/get-all-values-from-single-key-multiple-values-json-response-and-store-in-an-array/6094/2 "2021-05-15T07:02:53Z")

</div>

Hi All,

After a grinding trail and error method and lot of googling and repetitive runs. I am able to achieve the above scenario in Gatling.  
The high level solution is given below,

1. I stored the session value of templates as given below  
val laa: Vector[String] = session(" templates ").as[Vector[String]]

2. Then got the count of the vector array using the below  
val count1 = laa.size

3. Define i val and set it in a session

4. Check statement between i val and the array size to repeat the below code block for the **_times of array size_**

dowhile(session =\> (session(“i”).as[String].toInt \< session(“count1”).as[String].toInt)(

.exec(http(“A”)  
.get("/B/C/${laa(i)}/D/") // For each iteration all the values in the array will be executed one by one  
.headers(F)  
.check(status.is(200))))

// incrementing i

.exec(  
session =\> {  
i += 1  
println("i is ")  
println(i)  
session.set(“i”, i)  
}

)

Regards,  
Aravind G

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 15, 2021, 12:02pm UTC](https://community.gatling.io/t/get-all-values-from-single-key-multiple-values-json-response-and-store-in-an-array/6094/3 "2021-05-15T12:02:24Z")

</div>

It looks like you’ve reimplemented foreach: [https://gatling.io/docs/current/general/scenario/#foreach](https://gatling.io/docs/current/general/scenario/#foreach)
