cancel
Showing results for 
Search instead for 
Did you mean: 

How do I assert one of the objects in an array meets a condition in SoapUI by using JSONPath

SOLVED
Highlighted
Occasional Contributor

How do I assert one of the objects in an array meets a condition in SoapUI by using JSONPath

Let's say I have a response like below

 

{"errors": [{
   "code": "100",
   "message": "Message for 100",
  },

  {
   "code": "200",
   "message": "Message for 200",
  }

]}

 

I can assert the codes by using JSONPath like below:

 

  Expression: $.errors[0].code

  Expected result: 100

 

  Expression: $.errors[1].code

  Expected result: 200

 

The problem is that the order of the objects in the array may change, i.e. 200 occurs first and 100 occurs after. So, I have to assert something like "the errros should contain an error with code 100 while the error 100 can occur anywhere in the errors array". How do I do this?

 

Currently, I have to use Script assertion to achieve this and I am wondering if there is a better way.

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

That is a problem we have struggled with as well - I think this solution will work too, if you use expected result '100' for the following jsonpath, it should evaluate true if code 100 is present in the sample json. Basically it is asking to return the code value for any errors element where the .code subelement equals 100...  Of course this will run into problems if there can be multiple errors.code items in the json, but it has proved very useful in our tests.

 

 

$..errors[?(@.code==100)].code

 

 

Json used

{"errors": [{
"code": "100",
"message": "Message for 100"
},
{
"code": "200",
"message": "Message for 200"
}
]}

 

 

 

 

View solution in original post

6 REPLIES 6
Highlighted
Occasional Contributor

That is a problem we have struggled with as well - I think this solution will work too, if you use expected result '100' for the following jsonpath, it should evaluate true if code 100 is present in the sample json. Basically it is asking to return the code value for any errors element where the .code subelement equals 100...  Of course this will run into problems if there can be multiple errors.code items in the json, but it has proved very useful in our tests.

 

 

$..errors[?(@.code==100)].code

 

 

Json used

{"errors": [{
"code": "100",
"message": "Message for 100"
},
{
"code": "200",
"message": "Message for 200"
}
]}

 

 

 

 

View solution in original post

Highlighted
Occasional Contributor

Thanks a lot, Ton646.

 

Your solution works! I might stick with the scripting approach though since I have quite few validations and copy & paste of a customized function call is easier than creating multiple JSONPath Match assertions.

 

While I were validating your solution, I finally figured out why my attempts of the similar approach didn't work. My previous tries were using the following JSONPath:

 

$..errors[?(@.code=="100")].code

 

It seemed that this expression considered the quote character (") as part of the value for comparison.

Highlighted
Occasional Contributor

By the way, to get around the problem with multiple items, you can use "JSONPath Count" or "JSONPath Existence Match" instead of "JSONPath Match" as you see fit to assert the "number of occurences" or "existence". So the JSONPath assertions in SoapUI does meet my needs.

Highlighted
Occasional Contributor


@andrew_net wrote:

By the way, to get around the problem with multiple items, you can use "JSONPath Count" or "JSONPath Existence Match" instead of "JSONPath Match" as you see fit to assert the "number of occurences" or "existence". So the JSONPath assertions in SoapUI does meet my needs.


Hi - thanks for the suggestion - you are of course correct for cases where the expected count is known beforehand. The JSONPath Count assert didn't work for us in cases where it can be '1 or more' (ie - when it is unpredictable how many occurrences there might be). I would love an assert like xpath where you can do count(  //blah ) >= x.   But then we should have stuck with XML I guess 🙂

 

Regards,

Ton

Highlighted
Occasional Contributor

How about the "JSONPath Existence Match"? If you only want to assert something shows up and don't really care how many times it shows up, then "JSONPath Existence Match" may be of use. If you do care about how many times it shows up, then you know the expected number and you can use the "JSONPath Count".

 

Any way, I found SoapUI is not really stable. I tested "JSONPath Existence Match" approach yesterday many times and verified it worked, and then I found out it doesn't work any more just now. Or maybe I was confused???

 

Go back to my original example, I create two "JSONPath Existence Match" as below:

 

JSONPath: $..errors[?(@.code==100)]

Expected result: true

 

JSONPath: $..errors[?(@.code==800)]

Expected result: false

 

When I tested it yesterday, the first one was VALID and the second one was FAILED yesterday. And now both are VALID. It doesn't really matter what value I used for comparison (100, 800, or XYZ), it always return true!!! It seems that the "JSONPath Existence Match" only fails if you write something completely wrong, e.g.:

 

$..errors[3000]

or

$.errors[0].hello

or

$..nobodyhome

 

Maybe it doesn't support condition in "JSONPath Existence Match"? It does support condition in "JSONPath Count" and "JSONPath Match" though.

 

 

 

Highlighted
Occasional Contributor

I have only used the JsonPath Existence match to check if a particular node appears, like say: JSONPath Exists: $.data[0].title Im not sure it can be used for more complex JSONPath?   It would be great (in general) if smartbear could create some detailed, practical examples !  

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors