I shared csv2json.

From time to time I find myself with a CSV file with data that I’d like to manipulate/use with some program that is comfortable with JSON, like e.g. jq. Other times I need the reverse, e.g. I’d like to explore an array of JSON-encoded data with a program comfortable with spreadsheets/Comma Separated Value files.

I suspect there are a ton of such programs around, but why not reinvent this wheel on a Sunday afternoon, complete with overkill features I’m not going to need and shiny documentation?

So enter csv2json! Examples from the documentation:

# CSV to JSON using standard filehandles
$ csv2json <file.csv >file.json

# inputs and outputs can be specified explicitly
$ csv2json -i file.csv -o file.json

# also to explicitly set input and output with file name "-"
$ cat file.csv | csv2json -i - -o - > file.json

# the CSV can be provided as a command-line parameter
$ csv2json --csv "$csv_string"

# separator is ";", can be changed with a percent-encoded string
$ csv2json --sep %2C     # ","

# the end-of-line character can be set with a percent-encoded string
$ csv2json --eol %0D%0A  # "\r\n"

# the inverse operation is possible too
$ csv2json --inverse -i file.json -o file.csv

# this can be triggered automatically with proper naming
$ ln -s /path/to/csv2json /path/to/json2csv
$ json2csv -i file.json -o file.csv

# the JSON string can be provided as a command-line parameter
$ csv2json --inverse --json "$json_string"

For installation, I’ll probably go with the bundled version that includes module Text::CSV_PP:

cd ~/bin
curl -Lo csv2json \
chmod +x csv2json
ln -s csv2json json2csv

The last command creates a link that enables the inverse option out of the box, for better readability and easier typing.

At this point… stay safe and that’s all folks!

Comments? Octodon, , GitHub, Reddit, or drop me a line!