# Save More Than Value From JSON

**URL:** <https://community.gatling.io/t/save-more-than-value-from-json/4563>\
**Category:** Gatling (Open-Source)\
**Created:** [October 10, 2018, 11:08pm UTC](https://community.gatling.io/t/save-more-than-value-from-json/4563 "2018-10-10T23:08:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coverbec](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@coverbec](https://community.gatling.io/u/coverbec)\
**Post date:** [October 10, 2018, 11:08pm UTC](https://community.gatling.io/t/save-more-than-value-from-json/4563/1 "2018-10-10T23:08:36Z")

</div>

Hi,

I want to extract 2 properties from a random JSON object in an array for use in subsequent http calls. How can I do that?

Let’s say the JSON response is this:

`  
[  
{ “field1”: 1,  
“field2”: 2,  
“field3”: 3  
},  
{  
“field1”: 4,  
“field2”: 5,  
“field3”: 6  
}  
]

`

And I need to extract field1 and field2 from a random object and put them in the session. I’ve tried:

`  
…  
.check(jsonPath("$[\*][‘field1’, ‘field2’]").findRandom.saveAs(“row”)))

…

`

When I do this, only the value of field1 is saved to “row”.

Alternatively, I suppose I could store the whole object in row, but I’m not clear how to extract the fields I’m interested in afterwards.

Any ideas?

Thanks!

Charles

---

<div class="post-metadata">

**Author:** ![pujitha](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@pujitha](https://community.gatling.io/u/pujitha)\
**Post date:** [October 12, 2018, 10:42am UTC](https://community.gatling.io/t/save-more-than-value-from-json/4563/2 "2018-10-12T10:42:16Z")

</div>

Hi Charles,

I had a similar problem in past. I also had a problem of bulkey logs when the response is not in the json format when I use jsonPath(). I moved into using regex to sort that out.  
I use .SaveAs() to save the values into session like below.

.check(regex("""“f\_name”:\s\*"([^"]\*)""").saveAs(“firstName”))

Hope this helps.

-Pujitha.

---

<div class="post-metadata">

**Author:** ![coverbec](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@coverbec](https://community.gatling.io/u/coverbec)\
**Post date:** [October 15, 2018, 4:57pm UTC](https://community.gatling.io/t/save-more-than-value-from-json/4563/3 "2018-10-15T16:57:52Z")

</div>

Hi Pujitha,

Thanks for the reply… Your example only shows saving one value, but I guess I could capture two groups and save them as a tuple.

I ended up just doing two jsonPath statements, because I find the regex hard to read. Because one of the fields was an id and unique (which I didn’t show in my simplified example in my original post), I could use the value of the first id in the second statement:

`  
.check(jsonPath("$[\*].id").findRandom.saveAs(“id”))  
.check(jsonPath("$[?(@.id == ${id})].another\_field").saveAs(“another\_field”)

`

This may not be the most efficient way, but it works.

Thanks,

Charles

---

<div class="post-metadata">

**Author:** ![Serhii\_Aksiutin](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Serhii\_Aksiutin](https://community.gatling.io/u/Serhii_Aksiutin)\
**Post date:** [March 7, 2020, 7:39am UTC](https://community.gatling.io/t/save-more-than-value-from-json/4563/4 "2020-03-07T07:39:36Z")

</div>

Thanks that worked for me with UUID example:

`

.check(jsonPath("$.result.assetDetails[\*].id").findRandom.saveAs(“qnaUUID”))  
.check(jsonPath("$.result.assetDetails[?(@.id == “${qnaUUID}”)].metadata[0].value").saveAs(“assUUID”)))

`
