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
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
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
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
Many thanks, Nicolas,
your code was a great help for me