Unable to filter for null values with JSONpath
I am attempting to apply some assertions against a JSON response where some of the field values are null (6/10 results in my example). In the example I can successfully filter using, to return the colorCategoryId values of the results that have colorCategoryId=7:
$.result.[?(@.colorCategoryId=='7')].colorCategoryId
if I use the same filter to check for null I get no results returned
$.result.[?(@.colorCategoryId=='null')].colorCategoryId
also the filter $.result.[?(@.colorCategoryId!='null')].colorCategoryId returns [null, 5, null, 7, null, 7, null, 3, null, null]???
How can I use a JSONpath match assertion to filter for null values? I have tried a variety of different things (including assigning a null value to a property and using a property expansion in the filter) but cannot find any way to filter for these items will null values. Thank you for your help.
Example JSON below:
{ "isSuccess": true, "message": "", "exception": "", "result": [ { "id": 100744, "isActive": true, "colorCategoryId": null, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" }, { "id": 100867, "isActive": true, "colorCategoryId": 5, "assignees": [], "mediaTypeId": 2, "mediaTypeName": "OPT" }, { "id": 101392, "colorCategoryId": null, "assignees": [], "mediaTypeId": 2, "mediaTypeName": "OPT" }, { "id": 101395, "colorCategoryId": 7, "assignees": [], "mediaTypeId": 2, "mediaTypeName": "OPT" }, { "id": 101734, "colorCategoryId": null, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" }, { "id": 101738, "colorCategoryId": 7, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" }, { "id": 101753, "colorCategoryId": null, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" }, { "id": 101175, "colorCategoryId": 3, "assignees": [], "mediaTypeId": 2, "mediaTypeName": "OPT" }, { "id": 101445, "colorCategoryId": null, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" }, { "id": 101450, "colorCategoryId": null, "assignees": [], "mediaTypeId": 1, "mediaTypeName": "OP" } ], "pager": { "itemCount": 35, "pageIndex": 1, "pageSize": 10, "pageCount": 4 } }
$.result.[?(@.colorCategoryId==null)].colorCategoryId works with JSONPath Online Evaluator - jsonpath.com but not JSONpath match assertion for some reason.
A script assertion as a workaround could find all results with null
import groovy.json.JsonSlurper
json = new JsonSlurper().parseText(messageExchange.response.contentAsString)
nullcategory = json.result.findAll {it.colorCategoryId == null}
assert nullcategory.size() == 6
log.info nullcategory.id