What is the correct way to compare jsonPath's value as an integer

2,767 views
Skip to first unread message

Ashwin Jayaprakash

unread,
Apr 15, 2015, 9:13:15 PM4/15/15
to gat...@googlegroups.com
Hi, I would like to perform a simple check that verifies that the JSON response contains an integer that is greater than 0.
I've seen numerous examples that do string checks but nothing using integers.

How do I do something like this:
check(status.is(200), jsonPath("$.hits.total").asInstanceOf[Int] > 0)

Thanks.

Martin Nilsson

unread,
Apr 16, 2015, 7:18:35 AM4/16/15
to gat...@googlegroups.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/d/optout.

Ashwin Jayaprakash

unread,
Apr 16, 2015, 10:21:50 AM4/16/15
to gat...@googlegroups.com
Oh, I didn't realize that we were supposed to assert inside the expression. Having this somewhere in the docs right along with the HTTP status check would be very useful to first timers.

Thanks!

Pierre DAL-PRA

unread,
Apr 16, 2015, 10:34:49 AM4/16/15
to gat...@googlegroups.com
Asserting in the expression is not mandatory, as this works too : 

 jsonPath("$.hits.total").ofType[Int].greaterThan(5)

Ian Fordyce

unread,
Nov 14, 2018, 11:23:01 AM11/14/18
to Gatling User Group
Hi Pierre - hoping that this thread is not too stale...

I have a similar requirement (needing to check prometheus metrics during the test) and I've tried this approach, but I'm getting the response:

16:13:07.867 [gatling-http-thread-1-2] WARN  i.gatling.http.ahc.ResponseProcessor - Request 'get prometheus metric - systemload_average' failed: jsonPath($..value[1]).find.greaterThan(0.0), but actually can't compare nothing and 0.0
1.67

My request looks like this (developing in Intellij):

def getPrometheusMetric(metricName: String, namespace: String) = scenario(s"get prometheus metric - $metricName")
.exec(http(s"get prometheus metric - $metricName")
.get(s"/api/v1/query")
.queryParam("query", s"$metricName{namespace=\"$namespace\"}")
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.check(status.is(HttpStatus.SC_OK))
.check(bodyString.saveAs("sBodyString"))
.check(jsonPath("$..value[1]").saveAs("metricValue"))
.check(jsonPath("$..value[1]").ofType[Double].greaterThan(0.0))
)
.exec { session =>
println( session( "metricValue" ).as[String] )
session}

The error message seems to indicate that there is nothing returned by the json path, but I can see that the metric is being collected because it is printed out immediately after the check (1.67)

This has been driving me mad for weeks...

Thanks in advance.

Jon Thor Austen

unread,
Nov 14, 2018, 11:25:55 AM11/14/18
to gat...@googlegroups.com
ive noticed a similar issue and in one of those cases i had luck putting a asterisk wildard on my locator string.  not ideal but it got me unblocked.

Ian Fordyce

unread,
Nov 14, 2018, 11:36:44 AM11/14/18
to Gatling User Group
Thanks for the quick response Jon!

Sorry but my json path abilities are not great - how do I use the asterisk in this request?

Jon Thor Austen

unread,
Nov 14, 2018, 12:16:10 PM11/14/18
to gat...@googlegroups.com
what I mean is,  instead of   "$..field[0]"   you could try  "$..*.field[0]"  .   Or be more specific like "$.whatever.*.field[0]" .  This is probably really inefficient but if it works for you , it might give you more information on your locator problem and it might get you unblocked.

Ian Fordyce

unread,
Nov 14, 2018, 1:11:19 PM11/14/18
to Gatling User Group
Thanks Jon. Much appreciated. I'll let you know how I get on...

slan...@gatling.io

unread,
Nov 14, 2018, 1:54:11 PM11/14/18
to gat...@googlegroups.com
Please properly read Pierre’s sample: you’re missing his «ofType[Int] » so you’re converting to String.
Reply all
Reply to author
Forward
0 new messages