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

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”
}

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

Cheers

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

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

https://gatling.io/docs/current/http/http_check/