Lightweight Python script to convert a CSV file into a JSON array. Designed to be minimal, dependency‑free, and easy to drop into any workflow or data pipeline.
- Zero dependencies (only uses Python standard library:
csv,json,sys). - Streams simple CSV input into a pretty‑printed JSON array (indent = 2).
- Works with any header row: each CSV row becomes a JSON object keyed by the column names.
- Unix‑friendly: pipe or redirect output wherever you need it.
- Python 3 (verify with
python3 --version).
Download or copy csv-to-json.py into your project or a scripts directory:
curl -O https://raw.githubusercontent.com/MooseOnDaLoose/csv-to-json/main/csv-to-json.pypython3 csv-to-json.py input.csvpython3 csv-to-json.py input.csv > output.jsonawk -F, 'NR==1 || $3 == "Active"' input.csv | tee filtered.csv | python3 csv-to-json.py filtered.csv > active.json-
This shell pipeline reads input.csv, filters rows, saves the filtered subset, converts it to JSON, and writes the JSON to active.json.
-
The first command
awk -F, 'NR==1 || $3 == "Active"' input.csvuses awk with comma as the field separator ($FS set to ,). -
The condition
NR==1 || $3 == "Active"ensures the header row (record number 1) is always kept, and any subsequent row whose third column exactly equals the string Active is included. Only those lines pass through awk’s output. -
The pipe to
tee filtered.csvduplicates the filtered output stream: one copy is written to a new CSV file namedfiltered.csvfor later inspection, while the same stream continues down the pipeline. The next commandpython3 csv-to-json.py filtered.csvruns a Python script (csv-to-json.py) and passesfiltered.csvas its argument; the script is expected to read that CSV and emit JSON to standard output. -
Finally, the redirection
> active.jsoncaptures the JSON output produced by the Python script and writes it intoactive.json, overwriting that file if it already exists.
The net effect: filtered.csv holds the retained CSV rows, and active.json holds their JSON representation.
- If no input filename is provided, the script exits immediately (raises
SystemExit). - If the file cannot be opened, Python will raise an
IOError/FileNotFoundError.
Given mockData.csv:
id,first_name,last_name,email,gender
1,Justin,Alexander,jalexander0@indiatimes.com,Male
2,Donna,Stone,dstone1@alexa.com,Female
Run:
python3 csv-to-json.py mockData.csv > outputMockData.jsonProduces outputMockData.json:
[
{
"id": "1",
"first_name": "Justin",
"last_name": "Alexander",
"email": "jalexander0@indiatimes.com",
"gender": "Male"
},
{
"id": "2",
"first_name": "Donna",
"last_name": "Stone",
"email": "dstone1@alexa.com",
"gender": "Female"
}
]Internally the script:
- Reads the first command‑line argument as the input CSV path.
- Wraps
open()withcsv.DictReaderto map rows to dictionaries using the header row. - Collects all rows into a list comprehension.
- Pretty‑prints with
json.dumps(..., indent=2)to stdout.
You can easily customize indentation or output format by editing the final print(json.dumps(...)) line.