jsonPath: retrieve items from array of objects

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

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…

Why do you use findAll? This will of course capture a sequence of all occurrences, even if there’s actually only one.

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

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 "$[*]").