# What is the best way to access a JSON Structure in Response body

**URL:** <https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958>\
**Category:** Gatling (Open-Source)\
**Created:** [April 15, 2014, 9:56am UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958 "2014-04-15T09:56:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_Ulrich\_Kriegel](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@E\_Ulrich\_Kriegel](https://community.gatling.io/u/E_Ulrich_Kriegel)\
**Post date:** [April 15, 2014, 9:56am UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958/1 "2014-04-15T09:56:13Z")

</div>

Hi there,  
I’m new to Scala and Gatling (2.0.0-SNAPSHOT) and look for the best way to access properties in a JSON Object.

The response-Body contains an Array of JSON Objects with properties ‘a’, ‘b’ and ‘c’, which is stored via

jsonPath(…).saveAs(“indexList”)

in the session.

In a second step we want to loop through this list with forever.  
forever("${indexList}", “element”){…}

What is the best way access the properties of ${element}

Thanks in advance

–Ulrich

---

<div class="post-metadata">

**Author:** ![Nicolas\_R](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@Nicolas\_R](https://community.gatling.io/u/Nicolas_R)\
**Post date:** [April 15, 2014, 10:13am UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958/2 "2014-04-15T10:13:35Z")

</div>

I would start by creating a case class:

case class Stuff(a:A, b:B, c:C)

Then, apply a transformation to only store the properties you want:

jsonPath(…).transform( list =\> Stuff( list(“a”) … ) ).saveAs(“indexList”)

Then, it’s easy to navigate in the code, and also, you don’t keep a reference of the JSON response. That last part will make you save a lot of memory.

regards  
Nicolas

---

<div class="post-metadata">

**Author:** ![E\_Ulrich\_Kriegel](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@E\_Ulrich\_Kriegel](https://community.gatling.io/u/E_Ulrich_Kriegel)\
**Post date:** [April 15, 2014, 11:27am UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958/3 "2014-04-15T11:27:15Z")

</div>

Thanks for the hint, Nicolas

I tried it:

case classStuff(a:String, b:String)

However,

.check(jsonPath("$.index").transform(list=\>Stuff(list(“a”), list(“b”))).saveAs(“providerStructList”))

results in the following error:

Multiple markers at this line

- type mismatch; found : String(“a”) required:

io.gatling.core.session.Session

What is missing?

regards

Ulrich

---

<div class="post-metadata">

**Author:** ![Nicolas\_R](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@Nicolas\_R](https://community.gatling.io/u/Nicolas_R)\
**Post date:** [April 15, 2014, 2:55pm UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958/4 "2014-04-15T14:55:22Z")

</div>

Sorry, I should have been more precise. Here is a fully compiling example:

case class Stuff(a:Int, b:Double)

.check(jsonPath("$.foo").ofType[Seq[Any]].transform(\_.flatMap{ l =\>  
if(l.size==2)  
Some(Stuff(l(0).asInstanceOf[Int], l(1).asInstanceOf[Double]))  
else  
None  
}).saveAs(“foo\_stuff”)))

Does this help ?

Cheers  
Nicolas

---

<div class="post-metadata">

**Author:** ![E\_Ulrich\_Kriegel](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@E\_Ulrich\_Kriegel](https://community.gatling.io/u/E_Ulrich_Kriegel)\
**Post date:** [April 15, 2014, 3:52pm UTC](https://community.gatling.io/t/what-is-the-best-way-to-access-a-json-structure-in-response-body/958/5 "2014-04-15T15:52:34Z")

</div>

Many thanks, Nicolas,  
your code was a great help for me
