How to add contains or startswith in jq How to add contains or startswith in jq unix unix

How to add contains or startswith in jq


It's easier to filter before converting the object to an array for @csv:

$ (echo "column1,column2,column3";   jq -r 'select(.column3 | startswith("ab"))          | [.column1, .column2, .column3]          | @csv' data.json) > test.csv$ cat test.csvcolumn1,column2,column3"hello","bye","abc"

But if you do want to convert to an array first, you then have to select using the appropriate array index:

jq -r '[.column1, .column2, .column3]       | select(.[2] | startswith("ab"))       | @csv' data.json

Note how I enclosed the echo and jq in a set of parenthesis so they both run in the same subshell, and the output redirection outside of it, instead of having to redirect the output of both commands. Also gets rid of the Useless Use Of Cat; jq takes input filenames as arguments. Even if it didn't, input redirection is better than cat.