jsonPath: retrieve items from array of objects

2,782 views
Skip to first unread message

Nol de Wit

unread,
Jun 20, 2016, 12:41:57 PM6/20/16
to Gatling User Group
All,

I'm having some trouble retrieving individual values from the response I'm analysing. The code below is brewed up from some examples i've ran into, but i haven't got much success so far. 

The problem is that i am not able to retrieve just 1 object of the object array below, but only the array (without the surrounding []'s ). The log statement prints the whole array of objects without the [] brackets, not just one of the objects inside the array.

My response has the following structure (an array of objects, right?):

[
 
{
   
"registerID": "1234",

   
"expectedDate": "2016-06-21T00:00:00+02:00",
   
"first": false,
   
"frequency": "M"
 
},
 
{
   
"registerID": "5678",

   
"expectedDate": "2016-06-21T00:00:00+02:00",
   
"first": true,
   
"frequency": "Y"
 
}
]


I'd like to walk loop through the objects, and select one of them for further processing (actually, i'd like to get a random entry, but for starters I'm looking at the individual entries first).

For that I'm doing this:

    val reports = exec(http("Open report")
     
.get("/api/reports/2016/${userId}")
     
.check(status.is(200))
     
.check(jsonPath("$").ofType[Seq[Any]].findAll.saveAs("repArr"))
   
).foreach("${repArr}", "item") {
     
exec(session => {
        val itemMap
= session("item").as[Seq[Any]]
        println
(itemMap)  // full list/array, not just one item is printed
        session
.set("tmp","for now")
   
})


So how should I split up the array in items...?

Regards,
Nol


Nol de Wit

unread,
Jun 21, 2016, 8:23:47 AM6/21/16
to Gatling User Group

I tried a slightly different approach today, but no partytime yet. What I've learned (by logging) is that the result of the jsonPath expression is a Vector containing a (1) Buffer. When I retrieve an element using the code below, I have the full Buffer, according to my log. This buffer contains the whole array of json objects, but without the [].

So on one hand the jsonPath expression is wrong, and on the other hand I don't know how to retrieve the Buffer (or actually it's content) from the array.

    val selecteerRapportage = exec(http("Selecteer rapportage voor openen en open")

     
.get("/api/reports/2016/${userId}")
     
.check(status.is(200))
     
.check(jsonPath("$").ofType[Seq[Any]].findAll.saveAs("repArr"))

   
).exec { session =>
     
for {
        array
<- session("repArr").validate[Seq[Any]]
        arrayPrint
= println("array: " + array)
        sizePrint
= println("size: " + array.size)
        entry
= array(Random.nextInt(array.size))
        entryPrint
= println(entry)
     
} yield session.set("profileCode", "")
   
}


Log output:
array: Vector(Buffer({"registerID": "1234", ...} , {"registerID": "5678", ... }))
size: 1
Buffer({"registerID": "1234", ...} , {"registerID": "5678", ... }))

So:
- is my jsonPath expression correct?
- are the [] in the response I'm analyzing causing the problem?
- how should I retrieve the buffer contents from the Vector, and retrieve a random object?

Obviously, my limited experience with Gatling/Scala is not helping here, sorry for that...



Op maandag 20 juni 2016 18:41:57 UTC+2 schreef Nol de Wit:

Stéphane LANDELLE

unread,
Jun 21, 2016, 8:28:56 AM6/21/16
to gat...@googlegroups.com
Why do you use findAll? This will of course capture a sequence of all occurrences, even if there's actually only one.

Stéphane Landelle
GatlingCorp CEO


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nol de Wit

unread,
Jun 21, 2016, 8:53:46 AM6/21/16
to Gatling User Group
Stéphane, 

The response that I'm analyzing is an array with mulitple json objects ( hence Seq[Any] ). I'd like to have all objects, and than select a random object from these.

Having said that... when I just made it 'find' i.s.o. 'findAll', I do get better results. My array is now only a Buffer (not a Vector containing a buffer). It has given me one of the entries randomly, which is great. 

Not sure if I understand when to apply find() and findAll() however...

I must say that with this modification it also retrieves an empty buffer for some requests, which results in various "IllegalArgumentExceptions (bound must be positive)". I'll see how I can get  rid of these (ideas welcome).

Nol


Op dinsdag 21 juni 2016 14:28:56 UTC+2 schreef Stéphane Landelle:

Stéphane LANDELLE

unread,
Jun 21, 2016, 9:34:28 AM6/21/16
to gat...@googlegroups.com
Not sure if I understand when to apply find() and findAll() however...


That's pretty clear in the doc.
  • find returns the first occurence of what's matching the selector
  • findAll returns a sequence of all the occurrences of what's matching the selector
In your case, occurrences are Arrays, so the former gives you the first one (that happens to be the only one), hence an array, while the latter gives a sequence of array with only one entry.

You're probably confusing the array itself (that you fetch with "$") and its children (that you fetch with "$[*]").
Reply all
Reply to author
Forward
0 new messages