# How to extract values from a json chunk saved in a session variable?

**URL:** <https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987>\
**Category:** Gatling (Open-Source)\
**Created:** [February 9, 2021, 6:15pm UTC](https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987 "2021-02-09T18:15:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barun\_Mandal](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Barun\_Mandal](https://community.gatling.io/u/Barun_Mandal)\
**Post date:** [February 9, 2021, 6:15pm UTC](https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987/1 "2021-02-09T18:15:17Z")

</div>

Hi All,

I have used - .check(jsonPath("$.SubCategories[\*]").findRandom.saveAs(“SubCategory”)) and collected the below json response chunk in the session variable SubCategory. Now I want to extract **SubCategoryCName** and **random GroupCName** (using jsonpath if possible) in two session variables. What should be the best solution for this?

JSON response chunk (randomly captured from a response)

_{_  
_“Groups”: [_  
_{_  
_“GroupCName”: “mens—outright-winner”,_  
_“GroupName”: “Men - Outright Winner”,_  
_“Events”: null,_  
_“FixturesCount”: 0,_  
_“OutrightsCount”: 1_  
_},_  
_{_  
_“GroupCName”: “womens—outright-winner”,_  
_“GroupName”: “Women - Outright Winner”,_  
_“Events”: null,_  
_“FixturesCount”: 0,_  
_“OutrightsCount”: 1_  
_}_  
_],_  
_“SubCategoryCName”: “french-open”,_  
_“SubCategoryName”: “French Open”_  
_}_

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [February 10, 2021, 8:31am UTC](https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987/2 "2021-02-10T08:31:42Z")

</div>

Find your json path expression with something like this:  
[https://jsonpath.herokuapp.com/](https://jsonpath.herokuapp.com/)

Cheers

---

<div class="post-metadata">

**Author:** ![Barun\_Mandal](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Barun\_Mandal](https://community.gatling.io/u/Barun_Mandal)\
**Post date:** [February 10, 2021, 10:57am UTC](https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987/3 "2021-02-10T10:57:35Z")

</div>

Sorry, I wasn’t clear may be. I have already collected randomly a json chunk into a session variable and now I have a string as mentioned above saved in the SubCategory. I just want to extract values ( **SubCategoryCName** and **random GroupCName** ) from the session variable - SubCategory. I am unable to save the session variable value as a as[Seq[Map[String,Any]]]. It gives me the error with the below code

Code:  
val extractFromJsonTest = exec(session =\> {  
val SubCategoryjson = session(“SubCategory”).as[Seq[Map[String,Any]]]  
println("SubCategory Values: " + SubCategoryjson)  
session  
})

Error:

10:57:12.606 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - ‘hook-4’ crashed with ‘j.l.ClassCastException: Can’t cast attribute ‘SubCategory’ ‘{“Groups”:[{“GroupCName”:“antalya”,“GroupName”:“Antalya”,“Events”:null,“FixturesCount”:2,“OutrightsCount”:0},{“GroupCName”:“antalya—doubles”,“GroupName”:“Antalya - Doubles”,“Events”:null,“FixturesCount”:3,“OutrightsCount”:0},{“GroupCName”:“grenoble”,“GroupName”:“Grenoble”,“Events”:null,“FixturesCount”:5,“OutrightsCount”:0},{“GroupCName”:“grenoble—doubles”,“GroupName”:“Grenoble - Doubles”,“Events”:null,“FixturesCount”:4,“OutrightsCount”:0},{“GroupCName”:“monastir”,“GroupName”:“Monastir”,“Events”:null,“FixturesCount”:7,“OutrightsCount”:0},{“GroupCName”:“monastir—doubles”,“GroupName”:“Monastir - Doubles”,“Events”:null,“FixturesCount”:2,“OutrightsCount”:0},{“GroupCName”:“nur-sultan”,“GroupName”:“Nur-Sultan”,“Events”:null,“FixturesCount”:2,“OutrightsCount”:0},{“GroupCName”:“sharm-el-sheikh”,“GroupName”:“Sharm el Sheikh”,“Events”:null,“FixturesCount”:10,“OutrightsCount”:0},{“GroupCName”:“st-petersburg”,“GroupName”:“St Petersburg”,“Events”:null,“FixturesCount”:4,“OutrightsCount”:0},{“GroupCName”:“st-petersburg—doubles”,“GroupName”:“St Petersburg - Doubles”,“Events”:null,“FixturesCount”:2,“OutrightsCount”:0}],“SubCategoryCName”:“itf—men”,“SubCategoryName”:“ITF - Men”}’ of type class java.lang.String into interface scala.collection.immutable.Seq’, forwarding to the next one

---

<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:** [February 10, 2021, 2:12pm UTC](https://community.gatling.io/t/how-to-extract-values-from-a-json-chunk-saved-in-a-session-variable/5987/4 "2021-02-10T14:12:20Z")

</div>

Please have a proper look at the documentation, in particular ofType[T].

[https://gatling.io/docs/current/http/http\_check/](https://gatling.io/docs/current/http/http_check/)
