LinuxCommandLibrary

csvjson

CSV to JSON converter

TLDR

Convert CSV to JSON array

$ csvjson [data.csv]
copy
Convert with indentation for readability
$ csvjson -i [4] [data.csv]
copy
Convert to newline-delimited JSON (one object per line)
$ csvjson --stream [data.csv]
copy
Use a column as key for object output
$ csvjson -k [id] [data.csv]
copy
Convert to GeoJSON from lat/lon columns
$ csvjson --lat [latitude] --lon [longitude] [data.csv]
copy
Convert with custom delimiter
$ csvjson -d "[;]" [data.csv]
copy

SYNOPSIS

csvjson [options] file

DESCRIPTION

csvjson is part of csvkit that converts CSV files to JSON format. It supports various output formats including JSON arrays, keyed objects, newline-delimited JSON for streaming, and GeoJSON for geographic data.
By default, output is a JSON array of objects where each row becomes an object with column names as keys. The -k option creates a keyed object using a unique column's values as keys.
Type inference converts numeric and boolean values appropriately. The streaming mode outputs one JSON object per line, suitable for processing with tools like jq or for large files that shouldn't be loaded entirely.

PARAMETERS

FILE

CSV file to convert. Uses stdin if not specified.
-i N, --indent N
Indentation level for pretty printing.
-k COLUMN, --key COLUMN
Column to use as object keys (creates object instead of array).
--stream
Output newline-delimited JSON (one object per line).
--lat COLUMN
Latitude column for GeoJSON output.
--lon COLUMN
Longitude column for GeoJSON output.
-d CHAR, --delimiter CHAR
Field delimiter (default: comma).
-e ENCODING, --encoding ENCODING
Input file encoding.
--no-inference
Disable type inference (keep all values as strings).

CAVEATS

The -k option requires unique values in the key column. Large files may use significant memory without streaming mode. GeoJSON output requires valid latitude/longitude values. Type inference may misinterpret some values.

HISTORY

csvjson is part of csvkit, created by Christopher Groskopf in 2011. It enables easy conversion of tabular data to JSON, the dominant format for web APIs and JavaScript applications.

SEE ALSO

in2csv(1), jq(1), csvkit(1), miller(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community