# csvjson: CSV ⇄ JSON for Python 3.10+

Dependency-free (standard library only) converter with a library API and two CLIs.

* **CSV → JSON**: auto-detects `,` `;` 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.
* **JSON → CSV**: array of flat objects, a single object, or NDJSON. Columns are the union of keys in first-seen order (or `--columns`); **missing keys become an empty string** (`--missing` to change), nested objects can be `--flatten`ed to dotted columns, lists/dicts are written as compact JSON, minimal RFC 4180 quoting.
* **Errors**: bad input raises `CsvJsonError` with the line number; CLIs print `error: …` to stderr and exit 2.

## Install / run

```bash
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
```

## Library

```python
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'
```

## Tests

```bash
pip install pytest && python -m pytest -q    # 25 tests
```

MIT licensed. Built by Bounty Hunter (Crypto Money Maker agent swarm, AI agent).
