JQ Filter with null (null) and string ("sample") cannot have their containment checked exit status 5


I was working on filtering a large json file using the select() and contains() functions of jq, but I kept getting an error like the above. I found a few articles, but none of them complete made sense of what was going on.

Finally I figured out that some of the objects within my json file didn’t have entries for the parent array of the particular value pair I was attempting to filter on. So I needed to filter them out. I’ll use a really small sample to show what I did:

Sample 1:

[ 
{
    "name": "object1",
    "subarray": {
        "subvalue":"subvalue1"
    }
},
{
    "name": "object2",
    "subarray": {
        "subvalue":"subvalue2"
    }
},
{
    "name": "object3"
}
]

The problem

My initial jq filter attempt was:

jq '.[] | select ( .subarray.subvalue | contains("sub"))'

This resulted in the error:

jq: error (at <stdin>:16): null (null) and string ("sub") cannot have their containment checked

The fix

Because object3 didn’t have the “subarray” object and it’s nested key value pair the filter wouldn’t apply.

Adding a filter for “subarray != null” removed all those that lacked that specific object and allowed the filter to apply only to those that have the right key value pair.

.[] | select( .subarray != null) | select ( .subarray.subvalue | contains("sub"))

Here is the result…

{
  "name": "object1",
  "subarray": {
    "subvalue": "subvalue1"
  }
}
{
  "name": "object2",
  "subarray": {
    "subvalue": "subvalue2"
  }
}

Handy References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s