checkIf for jsonPath exists?

Hi.
Here is my code:
`

.check(
status.is(200),
jsonPath("$.coupons[?(@.picked==false)].id").findAll.saveAs(“coupous_id_list”)
)

`

But in some cases, jsonPath("$.coupons[?(@.picked==false)].id") will return null. So gatling will raise error: jsonPath($.coupons[?(@.picked==false)].id).findAll.exists, found nothing.

How do I omit this error if jsonPath("$.coupons[?(@.picked==false)].id") not exist?

I find a checkIf DSL, but I can’t add jsonPath("$.coupons[?(@.picked==false)].id") to checkIf(jsonPath("$.coupons[?(@.picked==false)].id").exists)(jsonPath("$.coupons[?(@.picked==false)].id").findAll.saveAs(“coupous_id_list”))

Any ideas?
Thanks.

And maybe saveAs could support a default value? Just like java System.getProperty(“property”, defaultValue).

Maybe jsonPath(“jsonPath”).findAll.saveAs(“var”, [])?

在 2017年3月5日星期日 UTC+8下午3:30:48,Feng Yu写道:

Check out transformOption as described here: Gatling - HTTP Checks

transformOption(function) takes a Option[X] => Validation[Option[X2]] function, meaning that it gives full control over the extracted result, even providing a default value.

And example usage:

`

transformOption(extract => extract.orElse(Some("default")).success)

`

Hope that helps :slight_smile:

Thanks. But I’m unfamiliar with Scala. Would you please show me a simple example? Thanks. :slight_smile:

在 2017年3月6日星期一 UTC+8下午9:32:37,adam.a…@pearson.com写道:

No problem. Here is an example from live scenario (user_level is set to 1 if attribute “number” is not found in JSON):

.check(jsonPath("$.hierarchy..[?(@.type=='Level')].attributes.number").transformOption(x => x.orElse(Some("1"))).saveAs("user_level"))

Great! Thanks very much!

For me this solution works very well:

sonPath("$.coupons[?(@.picked==false)].id").findAll.transformOption(x => x.orElse(Some(ArrayString))).saveAs(“coupous_id_list”)

doIf(// check coupous_id_list exists and not empty ) {
// steps
}

在 2017年3月6日星期一 UTC+8下午10:01:16,Artajew, Adam写道: