# Get data from json response array

**URL:** <https://community.gatling.io/t/get-data-from-json-response-array/942>\
**Category:** Gatling (Open-Source)\
**Created:** [April 9, 2014, 1:37pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942 "2014-04-09T13:37:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxime\_Tual](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Maxime\_Tual](https://community.gatling.io/u/Maxime_Tual)\
**Post date:** [April 9, 2014, 1:37pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/1 "2014-04-09T13:37:29Z")

</div>

Hi,

I’m working on Web Services who return data in JSON.  
Many of them returning data this way (simplify for the example):

{  
“items”: [  
{  
“id”: “123”,  
“name”: “test”  
},  
{  
“id”: “456”,  
“name”: “other”  
}  
],  
“pagingInfos”: {}  
}

I cannot know how many items it will have in items array, I want to get one random item from this array, then get is “id” for use it in another Web Service.

I found this in a previous post: [https://gist.github.com/slandelle/2469375](https://gist.github.com/slandelle/2469375)  
But I don’t succeed to change it for my conveniance, specially on the cast part. I got that for the moment:

val getAll = exec(http(“Get one random friend”)  
.get("/users/${user\_id}/friendships")  
.header(“Authorization”, “Bearer ${access\_token}”)  
.check(status.is(200), jsonPath("$.items").findAll.saveAs(“user\_friends”)))  
.exec((session: Session) =\> {  
val friends = session(“user\_friends”).as[List[String]]  
session.set(“random\_friend”, friends(math.max(random.nextInt(friends.length), 9)))  
})

The exception on the line “as[List[String]]”:

java.lang.ClassCastException: scala.collection.immutable.Vector cannot be cast to scala.collection.immutable.List  
at yelago.ws.GetUserFriendships$$anonfun$1.apply(GetUserFriendships.scala:22) ~[na:na]  
at yelago.ws.GetUserFriendships$$anonfun$1.apply(GetUserFriendships.scala:21) ~[na:na]  
at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35) ~[gatling-core-2.0.0-SNAPSHOT.jar:2.0.0-SNAPSHOT]  
at io.gatling.core.action.Failable$class.execute(Actions.scala:97) ~[gatling-core-2.0.0-SNAPSHOT.jar:2.0.0-SNAPSHOT]  
at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28) ~[gatling-core-2.0.0-SNAPSHOT.jar:2.0.0-SNAPSHOT]  
at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:30) ~[gatling-core-2.0.0-SNAPSHOT.jar:2.0.0-SNAPSHOT]

Any help ? 🙂 Thanx !

---

<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:** [April 9, 2014, 1:48pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/2 "2014-04-09T13:48:25Z")

</div>

Well, a Vector is simply not a List 😉  
Seq would be fine.

But then, Gatling EL has a random() builtin

You can use “${user\_friends.random()}”

---

<div class="post-metadata">

**Author:** ![Maxime\_Tual](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Maxime\_Tual](https://community.gatling.io/u/Maxime_Tual)\
**Post date:** [April 9, 2014, 2:08pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/3 "2014-04-09T14:08:24Z")

</div>

> Humm that random method in EL will be great if I make it work.

I have this first scenario:

val getAll = exec(http(“Get all friends”)  
.get(“/users/${user\_id}/friendships”)  
.header(“Authorization”, “Bearer ${access\_token}”)  
.check(status.is(200), jsonPath(“$.items”).findAll.saveAs(“user\_friends”))  
)

Follow by this second:

val getOneRandomFriend = exec(http(“Get one random friend”)  
.get(“/users/${user\_friends.random().id}”)  
.header(“Authorization”, “Bearer ${access\_token}”)  
.check(status.is(200))  
)

But got [ERROR] i.g.h.a.HttpRequestAction - No attribute named ‘user\_friends.random().id’ is defined  
Same error when I only put ${user\_friends.random()}

---

<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:** [April 9, 2014, 2:11pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/4 "2014-04-09T14:11:18Z")

</div>

No, “${user\_friends.random().id}” is invalid

Only “${user\_friends.random()}” is

---

<div class="post-metadata">

**Author:** ![Maxime\_Tual](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Maxime\_Tual](https://community.gatling.io/u/Maxime_Tual)\
**Post date:** [April 10, 2014, 1:44pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/5 "2014-04-10T13:44:23Z")

</div>

> No, “${user\_friends.random().id}” is invalid
> 
> Only “${user\_friends.random()}” is

The random() EL method not working either (maybe because user\_friends was not consider as list ? I don’t know)

Finally I end up using an external library call Dijon ([GitHub - jvican/dijon: A Dynamically Typed Scala Json Library](https://github.com/pathikrit/dijon)) which allow me to treat my JSON response as an object.

Thanx anyway 🙂

---

<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:** [April 10, 2014, 1:50pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/6 "2014-04-10T13:50:02Z")

</div>

jsonPath("$.items")

Why do you capture the whole object while you only need the id: jsonPath("$.[items.id](http://items.id)"), otherwise, as you wrote it, you’re capturing the full object String.

---

<div class="post-metadata">

**Author:** ![Maxime\_Tual](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Maxime\_Tual](https://community.gatling.io/u/Maxime_Tual)\
**Post date:** [April 10, 2014, 2:14pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/7 "2014-04-10T14:14:32Z")

</div>

> Because as I say in the first post my json response is more complex and I cannot just use a jsonPath to fill my need.

---

<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:** [April 10, 2014, 3:30pm UTC](https://community.gatling.io/t/get-data-from-json-response-array/942/8 "2014-04-10T15:30:45Z")

</div>

In both cases, you only want all the ids, not the full objects.  
What to do them them, either pick one randomly, or concat, comes in a second time.

capture: jsonPath("$.[items.id](http://items.id)").findAll.saveAs(“itemIds”)  
random: “${itemIds.random}”  
concat: session =\> session(“itemIds”).validate[Seq[String]].map(\_.mkString(",")) // or whatever, I don’t know how you want to concat

If you really want the objects: jsonPath("$.items").as[Map[String, Any]]

Don’t use another parser. In terms of performance, you’ll shoot yourself in the foot!
