Getting nested JSON array with JSONPath

535 views
Skip to first unread message

Nikita Kozhukhov

unread,
Oct 19, 2016, 4:08:56 PM10/19/16
to Gatling User Group
Hi! I'm sorry if this question has been answered before, but I couldn't find any solution so far.
The version of Gatling I'm using is 2.2.0-M2.
My goal is to get random values from JSON array. I was not able to parse the following JSON:
[
 
{
   
"site": "ABC",
   
"chemistry": [
     
{
       
"name": "first",
       
"height": 4,
       
"width": 6,
     
},
     
{
       
"name": "second",
       
"height": 8,
       
"width": 12,
     
}
   
]
 
},
 
{
   
"site": "DEF",
   
"chemistry": [
     
{
       
"name": "second",
       
"height": 8,
       
"width": 12,
     
}
   
]
 
}
]


using the following code:

object RandomValues {
val getRandomValues = exec(StaticResources.getJsonToParse
   
.check(status.is(200)).check(jsonPath("$[*].site").ofType[String].findAll.saveAs("siteJson"),
                                  jsonPath
("$[*].chemistry").findAll.saveAs("chemistryJson")))                   //(1)
   
.exec(session => {
      val rnd
= (new Random).nextInt(1)
     
      val siteFromJson
= session("siteJson").as[Seq[String]]
      val chemistryFromJson
= session("chemistryJson").as[Seq[Seq[Map[String, Any]]]]                           //(2)
     
      val site
= siteFromJson(rnd)
      val chemistry
= chemistryFromJson(rnd)


      session
})

}


Everything works fine for the "site" property, as it is a simple string field. But I can't parse "chemistry" - if I use
.ofType[Seq[Map[String, Any]]]

at line (1) I get error "No member of type class JsonFilter found for type Seq[Map[String,Any]]". If I try
.as[Seq[Seq[Map[String, Any]]]]

at line (2), I see "java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.Seq".

I've tried parsing json with Scala Jackson library, but with no success.
I feel like I'm missing something obvious and this problem should be easy to solve. 
Reply all
Reply to author
Forward
0 new messages