Ask a Question

Unable to filter for null values with JSONpath

SOLVED
jcskillings
New Contributor

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
   }
}
1 ACCEPTED SOLUTION

Accepted Solutions
PaulMS
Super Contributor

Re: Unable to filter for null values with JSONpath

$.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

 

View solution in original post

2 REPLIES 2
PaulMS
Super Contributor

Re: Unable to filter for null values with JSONpath

$.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

 

View solution in original post

jcskillings
New Contributor

Re: Unable to filter for null values with JSONpath

Yes, I have also noticed some discrepancies with various JSONpath evaluators online and how SoapUI handles JSONpath. It seems there are some differences in implementation. 

 

Thank you for the script example it worked well. I should also mention that in this case I found I was able to use an XPath Match assertion to filter for the null values as another workaround:

 

declare namespace ns1='https://excelsiorqa-api.eyekor.com/api/v1/Series';
//ns1:Response[1]/ns1:result[1]/ns1:e[ns1:colorCategoryId[@null='true']]/ns1:id

 

cancel
Showing results for 
Search instead for 
Did you mean: