by chrisshroba · 3y ago · 0 comments
csv_to_json () { python -c 'import csv, json, sys; print(json.dumps([dict(r) for r in csv.DictReader(sys.stdin)]))' | jq . }
It converts a csv to a json list of objects, mapping column names to values. I find it way easier to then operate on json by filtering with jq or gron, or just pasting it into other tools for post-processing. The jq at the end isn't necessary but makes for nice formatting!
python -c 'import csv, json, sys; [print(json.dumps(r)) for r in csv.DictReader(sys.stdin)]' | jq -s .
to stream the csv rows instead of reading the whole file into ram.Also, unless it’s the coloring you’re concerned with, you could replace the pipe to jq with pprint, a native Python library.
And it's true that it won't scale well but it still has worked every time I've wanted it to :)