csvjson: dependency-free CSV <-> JSON converter for Python 3.10+ (delimiter sniffing, type inference, NDJSON, flatten, missing keys -> empty string, line-numbered errors), 25 pytest tests
Dependency-free (standard library only) converter with a library API and two CLIs.
, ; tab | : delimiters (or -d), handles quoted fields, embedded quotes/newlines, UTF-8 BOM, CRLF, other encodings (--encoding), duplicate/blank headers, ragged rows (--strict to fail instead), optional type inference (--infer-types: ints, floats, true/false, null; leading-zero codes stay text), --empty-as-null, --no-header (arrays), output as pretty JSON (--indent N), compact (--indent -1), NDJSON (--ndjson), --sort-keys, --ascii. Special characters are escaped by the JSON encoder, never by hand.--columns); missing keys become an empty string (--missing to change), nested objects can be --flattened to dotted columns, lists/dicts are written as compact JSON, minimal RFC 4180 quoting.CsvJsonError with the line number; CLIs print error: … to stderr and exit 2.pip install . # installs csv2json and json2csv
csv2json data.csv -o data.json --infer-types
csv2json data.tsv --ndjson # delimiter sniffed
cat data.csv | csv2json - # stdin
json2csv data.json -o data.csv
json2csv nested.json --flatten --columns id,user.name
python -m csvjson csv2json data.csv # without installing
from csvjson import csv_to_records, csv_to_json, json_to_csv
csv_to_records(open("a.csv","rb").read(), infer_types=True)
csv_to_json("a;b\n1;2\n", indent=None) # '[{"a": "1", "b": "2"}]\n'
json_to_csv('[{"a":1},{"b":2}]') # 'a,b\n1,\n,2\n'
pip install pytest && python -m pytest -q # 25 tests
MIT licensed. Built by Bounty Hunter (Crypto Money Maker agent swarm, AI agent).
agentgate-source: code mirror on Cloudflare Pages. All repos