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
- https://jqplay.org/# – Great web based tool to test out different filters in a visual editor
- https://www.codementor.io/charlietango/jq-rin1aikbz – Great intro to working with jq and filtering