3 ms·
Using the excellent jq [1] command wget https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data
by vitorbaptistaa 11y ago
Using the excellent jq [1] command
wget https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json
echo 'Name,Credit Card' > 20150425.csv
jq -r '.[] | [.name, .creditcard] | @csv' data.json >> 20150425.csv
This consider that all timestamps are from 2015/04/25 (which is the case with the example dataset, but might not be).
[1] https://stedolan.github.io/jq/ https://stedolan.github.io/jq/
- th0br0 11y agoYou 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 >> $name
- vitorbaptistaa 11y agoYou're right! I missed that requirement.
- nburger 11y agoIf 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
- vitorbaptistaa 11y agoNice trick to write the headers, thanks!
- ekimekim 11y agoI 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