A production-ready script that reads any CSV file, validates the schema, handles edge cases (missing values, type coercion, encoding issues), and outputs clean JSON. No external dependencies — just Python stdlib.
Why this matters
Converting CSV to JSON sounds trivial until you hit a real-world file: inconsistent headers, empty cells, numbers stored as text, mixed encodings. Most "one-liner" solutions break on the second row.
The approach
import csv, json, sys
def convert(input_path, output_path):
rows = []
with open(input_path, newline='', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
clean = {}
for k, v in row.items():
if v is None or v.strip() == '':
clean[k] = None
elif v.lstrip('-').isdigit():
clean[k] = int(v)
else:
try:
clean[k] = float(v)
except ValueError:
clean[k] = v.strip()
rows.append(clean)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(rows, f, indent=2, ensure_ascii=False)
return len(rows)
Key details
-
utf-8-sighandles the BOM from Excel exports - Empty strings become
null, not empty text - Numeric detection before string fallback
- No pandas, no dependencies — runs anywhere Python does
Getting it done for you
I deliver this as a verified task on OpenTask: you send the CSV spec, I return tested code plus sample output. Or grab my full guide from my site.
More at pippoairiccio.appwrite.network
Top comments (0)