How to check if element exists inside check If by jsonPath?

Hi,

I need to check if values match only if they are present in the response

subscriptionId - I have a pre-stored number in feeder

Something like this:

.checkIf(jsonPath("$.items[0].subscriptionId") != null)(
         jsonPath("$.items[0].subscriptionId").is("#{subscriptionId}")
)

But as far as I understand, jsonPath("$.items[0].subscriptionId") is not null in any case, which is why such a condition simply does not work, and, unfortunately, I did not find any boolean functions to check for existence or did not understand how to work with them

Sample response body:

{
	"pageNumber": 1,
	"pageSize": 1,
	"total": 18,
	"items": [
		{
			"subscriptionId": number,
                        some other items
		}
	],
	"totalPages": 18,
	"hasPreviousPage": false,
	"hasNextPage": true
}

I would be very grateful for help on how to do this.

Have you tried:

.checkIf(jsonPath("$.items[0].subscriptionId")) {
jsonPath("$.items[0].subscriptionId").is("#{subscriptionId}")
}

?

Yes, but since the method accepts either Expression[Boolean] or Validation[Boolean] then it gives a compilation error to the construction you specified. I also forgot to add the gatling version, I have it 3.7.6
Error:

overloaded method checkIf with alternatives:
[error]   (condition: (io.gatling.http.response.Response, io.gatling.core.session.Session) => io.gatling.commons.validation.Validation[Boolean])(thenChecks: io.gatling.http.check.HttpCheck*): io.gatling.http.request.builder.HttpRequ
estBuilder <and>
[error]   (condition: io.gatling.core.session.Expression[Boolean])(thenChecks: io.gatling.http.check.HttpCheck*): io.gatling.http.request.builder.HttpRequestBuilder
[error]  cannot be applied to (io.gatling.core.check.CheckBuilder.MultipleFind[io.gatling.core.check.jsonpath.JsonPathCheckType,com.fasterxml.jackson.databind.JsonNode,String] with io.gatling.core.check.jsonpath.JsonPathOfType)     
[error]             .checkIf(jsonPath("$.items[0].subscriptionId"))(
[error]              ^
[error] one error found
[error] (Test / compileIncremental) Compilation failed

Several options you can try:

Sure this one works:

      .check(jsonPath("$.items[0].subscriptionId").optional.saveAs("foundSubscriptionId"))
      .checkIf("#foundSubscriptionId.exists()") {
        jsonPath("$.items[0].subscriptionId").is("#{subscriptionId}")
      }

Haven’t tried:

      .check(jsonPath("$.items[0].subscriptionId").withDefault(null).is("#{subscriptionId}"))
1 Like

Thank you very much for your help, I tried option 1 before, but for some reason it didn’t work out and in general I decided to discard it because of the extra buffer in the form of foundSubscriptionId, but since now it seems to be ok and it doesn’t work in another way, then so be it.
The second option gives an error:

jsonPath($.items[0].subscriptionId).find.transformOption.is(600904266) extraction crashed: transformOption crashed: j.l.NullPointerException: Cannot invoke “scala.Function1.apply(Object)” because “defaultValue$1” is null