Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add contains or startswith in jq

Tags:

linux

unix

jq

I have the following command:

echo "column1, column2, column3" > test.csv &&
cat data.json | jq -r '. | [.["column1"], .["column2"], .["column3"]] | @csv’ >> test.csv

It creates a column headings and the data from data.json.

I am trying to also add it where for example it only would pull data that contains the words ("abc") from column3.

I added |select(.column3| startswith ('ab')) so the full command is:

echo "column1, column2, column3" > test.csv &&
cat data.json | jq -r '. | [.["column1"], .["column2"], .["column3"]] |select(.column3| startswith ('ab')) | @csv’ >> test.csv

but I get the following error:

-bash: syntax error near unexpected token `('

my json.data looks like this:

{
      "column1": "hello",
      "column2": "bye",
      "column3": "abc"
}

How do I parse column3? Not sure what I am doing wrong.

like image 964
artemisia480 Avatar asked Mar 03 '23 04:03

artemisia480


1 Answers

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.csv
column1,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.

like image 182
Shawn Avatar answered Mar 05 '23 15:03

Shawn