Code Issues Releases
README.md
2790 bytes | 6b38a7e
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# csv-to-json

A lightweight, dependency-free CSV-to-JSON converter for AI agents and developer workflows.

## Features

- **Zero dependencies** — uses only Python's standard library
- **Type inference** — automatically converts numeric, boolean, and null values
- **Nested key expansion** — dot-notation headers (e.g., `user.name`) become nested JSON objects
- **Streaming support** — process large CSV files without loading them entirely into memory
- **Flexible input** — accepts file paths, raw CSV strings, or stdin
- **CLI and library usage** — use from the command line or import into your own code

## Quick Start

### Command Line

```bash
# Convert a file
python src/csv_converter.py data.csv -o output.json

# Pipe from stdin
cat data.csv | python src/csv_converter.py - --compact

# Use a tab delimiter and expand nested keys
python src/csv_converter.py records.tsv -d '\t' --expand-keys
```

### As a Library

```python
from src.csv_converter import csv_to_json

# From a file path
json_str = csv_to_json("data.csv")

# From a raw CSV string
csv_data = """name,age,active
Alice,30,true
Bob,25,false"""

result = csv_to_json(csv_data)
print(result)
# [
#   {"name": "Alice", "age": 30, "active": true},
#   {"name": "Bob", "age": 25, "active": false}
# ]
```

### Streaming Large Files

```python
from src.csv_converter import stream_csv_to_json

with open("large_dataset.csv") as f:
    for record in stream_csv_to_json(f):
        process(record)  # handle one record at a time
```

## CLI Options

| Flag | Description |
|------|-------------|
| `input` | Input CSV file path, or `-` for stdin (default: stdin) |
| `-o, --output` | Output JSON file path (default: stdout) |
| `-d, --delimiter` | CSV delimiter character (default: `,`) |
| `--no-type-inference` | Keep all values as strings |
| `--expand-keys` | Expand dot-notation headers into nested objects |
| `--compact` | Output minified JSON without indentation |

## Nested Key Expansion

Given a CSV with dot-notation headers:

```csv
user.name,user.email,user.address.city
Alice,alice@example.com,Portland
```

With `--expand-keys`, the output becomes:

```json
[
  {
    "user": {
      "name": "Alice",
      "email": "alice@example.com",
      "address": {
        "city": "Portland"
      }
    }
  }
]
```

## Type Inference Rules

When type inference is enabled (the default):

| CSV Value | JSON Type | Result |
|-----------|-----------|--------|
| `42` | integer | `42` |
| `3.14` | float | `3.14` |
| `true`, `yes` | boolean | `true` |
| `false`, `no` | boolean | `false` |
| _(empty)_ | null | `null` |
| anything else | string | `"..."` |

Disable with `--no-type-inference` to preserve all values as strings.

## Requirements

- Python 3.10+
- No external packages required

## License

MIT