csvkit csvjson: Convert CSV to JSON
csvjson turns a CSV into a JSON array of objects keyed by the header row, with options for indentation, NDJSON and key-by-column output.
Feeding a CSV into a JSON API or a jq pipeline starts with csvjson. It infers types, so numeric columns come out as numbers, not strings.
What it does
csvjson reads a CSV and emits a JSON array where each object maps header names to row values. --stream produces newline-delimited JSON, -k keys the output by a chosen column, and -i sets indentation.
Common usage
# CSV -> pretty JSON array
csvjson -i 2 data.csv
# newline-delimited JSON (one object per line)
csvjson --stream data.csv
# key the output object by the id column
csvjson -k id data.csvOptions
| Flag | What it does |
|---|---|
| -i, --indent <n> | Indent the JSON by n spaces |
| --stream | Emit newline-delimited JSON instead of an array |
| -k, --key <column> | Output an object keyed by this column |
| --no-inference | Disable type inference; keep everything as strings |
| -d, --delimiter <c> | Input delimiter |
| -e, --encoding <enc> | Input encoding |
In CI
Convert a CSV export into JSON and pipe straight into jq: csvjson data.csv | jq ".[] | select(.active)". Use --stream when the file is large so downstream tools process it line by line instead of buffering one huge array.
Common errors in CI
Type inference can surprise you: a zip code like 01234 becomes the number 1234, and True/False become booleans. Pass --no-inference to keep raw strings. -k id fails with KeyError style messages if the named column is missing or has duplicate values (duplicates overwrite each other in the keyed object). Non-UTF-8 input raises UnicodeDecodeError; set -e.