Converting large amounts of JSON to CSV on macOS
I want to convert some 20mb+ JSON files to CSV.
Tried using webapps like https://konklone.io/json/ or https://www.convertcsv.com/json-to-csv.htm but they just crash.
So how can I use other tools to do such conversions?
Solution 1:
jq
works quite well for this, you can install it via Homebrew. Specific usage depends on the structure of your JSON file, the accepted answer on https://unix.stackexchange.com/questions/570548/convert-json-array-into-csv has details which should help to get started.
If you don't have or use Homebrew you do not need it in order the get jq
, as a precompiled binary is available at https://stedolan.github.io/jq/. It's not packaged and once downloaded as e.g. jq-osx-amd64
, I did the following in Terminal:
cd Downloads
xattr -d com.apple.quarantine jq-osx-amd64
mv jq-osx-amd64 jq
chmod +x jq
mkdir -p /usr/local/bin
sudo mv -nv jq /usr/local/bin/
As /usr/local/bin/
is already in the PATH
/path
passed to the the shell
in Terminal, just typing jq
and pressing enter will run it.
The website has Tutorial and Manual links too.
Solution 2:
Other than some poor experiences you've had with the web-based converters, you've not indicated what your preferences are for programming language; e.g. Python, JavaScript, shell, etc. There are many options available, and it's really a matter of finding one that's in your "comfort zone".
Search engines are great for identifying options for something like this. For example, if you want to do this in Python, this search may help. A more language-neutral search may be to search github for JSON to CSV converters.
I tend to do these things from the command line, or in small scripts. I've tried jq
and I've tried Miller
(akamlr
). jq
has a larger following, but I have a preference for Miller
because I get results I need without too much effort... but YMMV. One other thing: If you decide to try Miller
you should file this link to the "Cookbook" page for Miller
... it's not advertised well, but I find it useful 'cause I'm a cook :)