json to csv using jq
The jq
library features a @csv
operator for which I recently found a use case. Here’s an example of how to use it:
Input json is an array in a file called myFile.json
:
Here’s out to turn it into csv:
The use of tr
is a bit of a hack. The -r
flag on jq
produces “raw” output, aka not formatted as JSON. However, the inner JSON strings retain their quotes. But its easy enough to remove with tr
It’s also easy to grab the keys for use as headers if required
Then just pipe each command into a file for clean csv