Skip to content

MooseOnDaLoose/csv-to-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

CSV to JSON Converter

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.

Features

  • 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.

Requirements

  • Python 3 (verify with python3 --version).

Installation

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.py

Usage

Basic conversion (writes JSON to stdout):

python3 csv-to-json.py input.csv

Redirect to a file:

python3 csv-to-json.py input.csv > output.json

Use in a pipeline (example: filter then convert):

awk -F, 'NR==1 || $3 == "Active"' input.csv | tee filtered.csv | python3 csv-to-json.py filtered.csv > active.json
  1. This shell pipeline reads input.csv, filters rows, saves the filtered subset, converts it to JSON, and writes the JSON to active.json.

  2. The first command awk -F, 'NR==1 || $3 == "Active"' input.csv uses awk with comma as the field separator ($FS set to ,).

  3. 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.

  4. The pipe to tee filtered.csv duplicates the filtered output stream: one copy is written to a new CSV file named filtered.csv for later inspection, while the same stream continues down the pipeline. The next command python3 csv-to-json.py filtered.csv runs a Python script (csv-to-json.py) and passes filtered.csv as its argument; the script is expected to read that CSV and emit JSON to standard output.

  5. Finally, the redirection > active.json captures the JSON output produced by the Python script and writes it into active.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.

Exit Conditions

  • If no input filename is provided, the script exits immediately (raises SystemExit).
  • If the file cannot be opened, Python will raise an IOError / FileNotFoundError.

Example

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.json

Produces 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"
  }
]

How It Works

Internally the script:

  1. Reads the first command‑line argument as the input CSV path.
  2. Wraps open() with csv.DictReader to map rows to dictionaries using the header row.
  3. Collects all rows into a list comprehension.
  4. 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.

About

Simple python CSV to JSON converter

Resources

Stars

Watchers

Forks

Contributors

Languages