Saving value from response json array via comparison

Hi,

Say I have the following json:

{
“field1”: 2,
“field2”: [
{
“foo”: “123”,
“bar”: “abc”
},
{
“foo”: “456”,
“bar”: “def”
}
]
}

and I want to save that foo value whose bar is ‘abc’, i.e I need to extract the value 123 in this example. How would I do that in gatling?

I tried referring to the examples in: http://goessner.net/articles/JsonPath/

and came up with: .check(jsonPath("$.field2[?(@bar=abc)]").saveAs(“fooArrayElement”))

to see if I could atleast extract:

{
“foo”: “123”,
“bar”: “abc”
}

and then probably further extract foo from this, but I got an error instead.

Also tried following, but it didn’t work:

.check(jsonPath("$[field2][?(@bar=abc)].foo").saveAs(“myFoo”))

Ok so I figured this out for static bar values, i.e below works for me:

.check(jsonPath("$[‘field2’][?(@. bar == ‘abc’]. foo").saveAs(“myFoo”))

however, if I try to replace ‘abc’ by a value stored in the session as a attribute e.g:

.check(jsonPath("$[‘field2’][?(@. bar == ‘${myBar}’]. foo").saveAs(“myFoo”))

it fails. Any idea how I could fix this?

Solved it, I was missing a ‘)’ above, it’s supposed to be:

.check(jsonPath("$[‘field2’][?(@. bar == ‘${myBar}’)]. foo").saveAs(“myFoo”))