Checking if results array contains value

11,409 views
Skip to first unread message

Jeff Storey

unread,
Jan 16, 2014, 4:24:10 PM1/16/14
to gat...@googlegroups.com
I have a web service that returns an array, and I'm trying to verify that the array contains a particular value as a check (Gatling 1.x). The array is the entire response (it's the root element).

I'm quite new to gatling so I'm having issues getting this to work. What I've tried is

check(jsonPath("$_").contains("someValue"))) // the check is chained to the rest of the exec call

I'm getting an error that says  error: value contains is not a member of com.excilys.ebi.gatling.http.check.HttpMultipleCheckBuilder[String]. I'm guessing this is happening because jsonPath doesn't return an array, but I'm not sure what the correct syntax is for this.

Thanks,
Jeff

Stéphane Landelle

unread,
Jan 16, 2014, 4:49:25 PM1/16/14
to gat...@googlegroups.com
Hi,

$_ is not a valid JsonPath expression. Please have a look at the spec http://goessner.net/articles/JsonPath and try some online tester, such as this one: http://jsonpath.curiousconcept.com

If you want to collect all the values, the correct path is "$..*". However, we don't currently have a "contains" check. The reason is that we expect users to do this filtering in the expression.

So, what you have to do is design a proper JsonPath expression that will only succeed when your condition regarding "someValue" holds, and use the regular/default "exists".

Cheers,

Stéphane




2014/1/16 Jeff Storey <store...@gmail.com>

--
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/groups/opt_out.

Jeff Storey

unread,
Jan 16, 2014, 4:54:02 PM1/16/14
to gat...@googlegroups.com
Thanks for the quick reply. The reason I was trying the underscore was from a note on the gatling wiki page that says 
In JSON, the root element has no name. This might be a problem when it's an array and one want to target its elements. As a workaround, Gatling names it _.

Am I misunderstanding what that means?

But to the rest of your reply, that makes sense. I'll give that a try - thanks again.

Stéphane Landelle

unread,
Jan 16, 2014, 4:57:30 PM1/16/14
to gat...@googlegroups.com
Ah, OK :)

That was a long long time ago, when we were using another syntax, more similar to XPath.
We switched along the way to the standard one.


2014/1/16 Jeff Storey <store...@gmail.com>

Jeff Storey

unread,
Jan 16, 2014, 4:58:03 PM1/16/14
to gat...@googlegroups.com
Got it, thanks for clarifying. I'll try the way you suggested.


--
You received this message because you are subscribed to a topic in the Google Groups "Gatling User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gatling/jOCUe9Dd534/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gatling+u...@googlegroups.com.

Jeff Storey

unread,
Jan 16, 2014, 5:43:50 PM1/16/14
to gat...@googlegroups.com
I think I still might be missing something. My returned data looks like:
["item1","item2","item3"]

And my check expression is
.check(jsonPath("$[?(@==\"item1\")]").exists))

That check is not passing.

But if i put it in that jsonPath tester (from the link you gave), then it works just fine...

Stéphane Landelle

unread,
Jan 16, 2014, 6:05:56 PM1/16/14
to gat...@googlegroups.com
It seems that this javascript implementation allows the use of double quotes instead of the regular simple ones, which we don't.

"$[?(@=='item1')]"

Jeff Storey

unread,
Jan 16, 2014, 6:09:11 PM1/16/14
to gat...@googlegroups.com
Worked perfectly, thanks.

Stéphane Landelle

unread,
Jan 16, 2014, 6:14:21 PM1/16/14
to gat...@googlegroups.com
Cool!
Have fun


2014/1/17 Jeff Storey <store...@gmail.com>
Reply all
Reply to author
Forward
0 new messages