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.
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"
}
]}