You forgot the select(.creditcard) - see my comment on the gist - as this one does not filter out null'ed creditcards:
name=`date +'%Y%m%d'`;
echo "name,creditcard" > $name;
jq -r '.[] | select(.creditcard) | [.name, .creditcard] | @csv' data.json >> $nameIf we're going for succinctness, you could combine these all together:
(echo '"Name","Credit Card"'; jq -r '.[] | select(.creditcard) | [.name, .creditcard] | @csv' data.json) >> `date +%Y%m%d`.csv
Re:CSV Challenge
by vitorbaptistaa on Saturday April 25, 2015 @10:47AM
(#9438439)
Nice trick to write the headers, thanks!
I used jq as well, though instead of doing a seperate echo I did this:
([{name: "name", creditcard: "creditcard"}] + .)[]
instead of .[]Another less hacky way would be:
[["name", "creditcard"]] + map(select(.creditcard) | [.name, .creditcard]) | .[] | @csv
Re:CSV Challenge
by vitorbaptistaa on Saturday April 25, 2015 @10:39AM
(#9438419)
You're right! I missed that requirement.
[1] https://stedolan.github.io/jq/