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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With