# Gatling: JsonPath extract multiple values

**URL:** <https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943>\
**Category:** Gatling (Open-Source)\
**Created:** [January 28, 2015, 3:24pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943 "2015-01-28T15:24:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_Gagnon-Adam](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bruno\_Gagnon-Adam](https://community.gatling.io/u/Bruno_Gagnon-Adam)\
**Post date:** [January 28, 2015, 3:24pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/1 "2015-01-28T15:24:19Z")

</div>

I’m building a gatling 2.1.3 scenario and I need to extract data from a json body.

Example of the body:

[  
{  
“objectId”: “FirstFoo”,  
“pvrId”: “413”  
“type”: “foo”,  
“name”: “the first name”,  
“fooabilities”: {  
“foo1”: true,  
“foo2”: true  
},  
“versions”: [23, 23, 23, 23, 23, 23, 24, 23, 23],  
“logo”: [  
{  
“type”: “firstlogo”,  
“width”: 780,  
“height”: 490,  
“url”: “firstlogos/HD/{resolution}.png”  
}  
]  
},  
{  
“objectId”: “SecondFoo”,  
“pvrId”: “414”  
“type”: “foo”,  
“name”: “the second name”,  
“fooabilities”: {  
“foo1”: true,  
“foo2”: false  
},  
“versions”: [23, 23, 23, 23, 23, 23, 24, 23, 23],  
“logo”: [  
{  
“type”: “secondlogo”,  
“width”: 780,  
“height”: 490,  
“url”: “secondlogos/HD/{resolution}.png”  
}  
]  
}  
]

and I have this code trying to extract de data:

exec(  
http(“get object”)  
.get(commons.base\_url\_ws + “/my-resource/2.0/object/”)  
.headers(commons.headers\_ws\_session).asJSON  
.check(jsonPath("$…\*").findAll.saveAs(“MY\_RESULT”))) (1)  
.exec(session =\> {  
foreach("${MY\_RESULT}", “result”) { (2)  
exec(session =\> {  
val result= session(“result”).as[Map[String, Any]]  
val objectId = result(“objectId”)  
val version = result(“version”) (3)  
session.set(“MY\_RESULT\_INFO”, session(“MY\_RESULT\_INFO”).as[List[(String,Int)]] :+ Tuple2(objectId, version)) (4)  
})  
}  
session  
})

My goal is:  
To extract the objectId and the 9th value from the version array.  
I want it to look as Vector → [(id1, version1),(id2, version2)] in the session to reuse later in another call to the API.

My concerns are:  
(1) Is this going to create entries in the session with the complete sub objects, because in other answers I was that is was always a map that was saved (“id” = [{…}]) and here I do not have ids.

(2) In the logs, I see that the session is loaded with a lot of data, but this foreach is never called.

(3) This is an array in the json but I can’t find how to extract data from it at the moment.

(4) I try to add the data to the already existing one since it is a foreach.

My experience in Scala is of a beginner so please point out if I have clear issues I did not see.

I have looked into this issue: [http://stackoverflow.com/questions/25289334/gatling-looping-through-json-array](http://stackoverflow.com/questions/25289334/gatling-looping-through-json-array) and it is not exactly answering my case.

Many Thanks!

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 28, 2015, 4:20pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/2 "2015-01-28T16:20:57Z")

</div>

How about creating two checks

`  
.check(jsonPath("""$…objectId""").findAll.saveAs(“objectids”))

`.check(jsonPath("""$..versions[8]""").findAll.saveAs("versions")) //only the 9th element will be saved here.`

`

Then in foreach, use whichever variable you want to.

---

<div class="post-metadata">

**Author:** ![Bruno\_Gagnon-Adam](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bruno\_Gagnon-Adam](https://community.gatling.io/u/Bruno_Gagnon-Adam)\
**Post date:** [January 28, 2015, 7:14pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/3 "2015-01-28T19:14:53Z")

</div>

I thought about this option but I need to use the id and the version as a tuple per jsonBlock. So the second id must be linked to the second version. So a foreach would be hard to implement since I don’t have an index on the current value.

I used a regex instead and got to this solution:

.check(regex("""(?:“objectId”|“version”):"(._?)",._?(?:“objectId”|“version”):[(?:._?,){9}([0-9]_?),.\*?]""").ofType[(String, String)].findAll saveAs (“OBJECTS”)))

It’s working fine but I would have enjoyed to stick with jsonPath for extracting this information.

thanks for you reply!

---

<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:** [January 28, 2015, 7:49pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/4 "2015-01-28T19:49:40Z")

</div>

I thought about this option but I need to use the id and the version as a

> tuple per jsonBlock. So the second id must be linked to the second version.  
> So a foreach would be hard to implement since I don't have an index on the  
> current value.

Do a repeat \<[http://gatling.io/docs/2.1.4/general/scenario.html#repeat&gt](http://gatling.io/docs/2.1.4/general/scenario.html#repeat&gt);  
loop of the size  
\<[http://gatling.io/docs/2.1.4/session/expression\_el.html#expression-language&gt](http://gatling.io/docs/2.1.4/session/expression_el.html#expression-language&gt);  
of the objectids, where you explicitly set the name of the loop index.  
Then use this index  
\<[https://github.com/gatling/gatling/blob/master/gatling-core/src/test/scala/io/gatling/core/session/el/ElSpec.scala#L158&gt](https://github.com/gatling/gatling/blob/master/gatling-core/src/test/scala/io/gatling/core/session/el/ElSpec.scala#L158&gt);  
to fetch the proper element from each collection.

> .exec(session =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach

No way this would work. You can't use DSL elements instead those functions.

\*Stéphane Landelle\*  
\*Lead developer\*  
slandelle@gatling.io

---

<div class="post-metadata">

**Author:** ![Bruno\_Gagnon-Adam](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bruno\_Gagnon-Adam](https://community.gatling.io/u/Bruno_Gagnon-Adam)\
**Post date:** [January 28, 2015, 8:01pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/5 "2015-01-28T20:01:50Z")

</div>

Awesome, you just fixed my problem!  
Merci Stéphane!

---

<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:** [January 28, 2015, 8:27pm UTC](https://community.gatling.io/t/gatling-jsonpath-extract-multiple-values/1943/6 "2015-01-28T20:27:25Z")

</div>

De rien!
