This is close to this question: jq filter JSON array based on value in list , the only difference being the condition. I don't want equality as a condition, but a "contains", but I am struggling to do it correctly.
The problem: If want to filter elements of an input file based on the presence of a specific value in an entry The input file:
[{
"id":"type 1 is great"
},{
"id":"type 2"
},
{
"id":"this is another type 2"
},
{
"id":"type 4"
}]
The filter file: ids.txt:
type 2
type 1
The select.jq file:
def isin($a): . as $in | any($a[]; contains($in));
map( select( .id | contains($ids) ) )
The jq command:
jq --argjson ids "$(jq -R . ids.txt | jq -s .)" -f select.jq test.json
The expected result should be something like:
[{
"id":"type 1 is great"
},{
"id":"type 2"
},
{
"id":"this is another type 2"
}]
The problem is obviously in the "isin" function of the select.jq file, so how to write it correctly to check if an entry contains one of the string of another array?