CSV to JSON in Python
Convert CSV samples into JSON, compare the output with Python csv and pandas workflows, and verify row shape before writing import code.
Paste your JSON → Get results instantly (no signup)
→ Parse this CSV text into JSON rows.
1[2 {3 "name": "Alice",4 "age": 30,5 "city": "New York"6 },7 {8 "name": "Bob",9 "age": 25,10 "city": "London"11 }12]Love the result?
Use this exact pipeline in your app, backend, or LLM workflow.
No setup needed. Works with curl, Node, Python.
Uses example data. For edited input, copy from the playground.
Works with:
- Python csv.DictReader
- pandas records output
- Backend import scripts
- Data engineering samples
Example: input → output
CSV to JSON in Python
Python CSV to JSON conversion usually means reading rows with csv.DictReader or pandas, then writing a JSON array of dictionaries. This page helps you preview that output shape before you turn the conversion into a backend script, data pipeline, notebook, or import job.
It uses the same engine as the CSV to JSON Converter, but the intent is Python-specific: compare browser output with the objects your script should produce, catch header problems early, and decide whether type inference should happen before or after Python reads the file.
Python csv module example
import csv
import json
with open("users.csv", newline="") as f:
rows = list(csv.DictReader(f))
print(json.dumps(rows, indent=2))csv.DictReader maps header names to dictionary keys. That is the same conceptual mapping this page shows: each CSV row becomes one JSON object.
pandas CSV to JSON example
import pandas as pd
df = pd.read_csv("users.csv")
json_text = df.to_json(orient="records")Pandas is useful when you need type coercion, column cleanup, filtering, or large data-frame operations. The browser converter is faster when you only need to inspect a sample, copy fixture JSON, or confirm that headers produce the expected keys.
Python vs online conversion
Use Python when conversion is automated, scheduled, versioned, or too large for a browser workflow. Use this online tool when you are exploring a file, debugging quoted CSV, checking headers, or preparing a small JSON sample for tests.
For API imports, see CSV to JSON API. For browser-only conversion, use Convert CSV to JSON online. For big files, use Large CSV to JSON to think through batching and memory.
Python conversion pitfalls
csv.DictReaderreturns strings unless you add type conversion yourself.- Duplicate headers can silently create confusing dictionaries.
- Empty cells need a deliberate policy: empty string,
None, or omitted key. - Pandas may infer types differently from your API contract.
- Large files should stream or chunk instead of building one huge list.
Use the browser converter as a quick expected-output preview, then make the Python script match that shape deliberately.
Related conversions
- CSV to JSON JavaScript - compare JavaScript parsing and row-object output.
- CSV to JSON with headers - map header names to object keys.
- CSV to JSON without headers - handle files that need generated column names.
- CSV to JSON array - understand the array-of-objects output shape.
Frequently asked questions
How do I convert CSV to JSON in Python?+−
Use csv.DictReader for standard-library conversion or pandas.read_csv with to_json(orient="records") for dataframe workflows.
Is Python better than an online CSV to JSON converter?+−
Python is better for automated or repeated jobs. The online converter is faster for inspecting samples, debugging headers, and copying fixture JSON.
Does Python infer JSON types automatically?+−
csv.DictReader returns strings by default. pandas can infer types, but you should review numeric, boolean, and null handling before exporting JSON.
Can I use this page before writing Python code?+−
Yes. It helps confirm the expected array-of-objects shape before you implement the same mapping in Python.
What should I check before using CSV output in Python?+−
Check header names, duplicate columns, quoted commas, missing values, and whether downstream code expects strings or typed values.
Related tools
- JSON to CSVConvert JSON array row data into final CSV text output
- Format ValuesReformat individual values with case changes, trimming, coercion, and slugification
- Map ValuesRemap existing values through a lookup table such as enums, codes, or category names
- Compute FieldCreate derived values or fields from formulas, expressions, and simple conditionals
Read more on the blog
Advanced usage (optional)
CSV to JSON
v1.0.0Description
CSV to JSON
Parse CSV text into a JSON array of objects. Supports multiple delimiters, automatic type inference, header row detection, and whitespace trimming.
How It Works
The utility reads CSV text (string input) and converts each row into a JSON object. Column names come from the header row (if enabled) or are auto-generated as col1, col2, etc.
Type Inference
When enabled, the parser automatically converts values:
"30"→30(number)"true"/"false"→true/false(boolean)- Empty values →
""(empty string)
Disable type inference to keep all values as strings.
Configuration
| Field | Type | Default | Description | |
|---|---|---|---|---|
| Delimiter | enum | , | Field separator: ,, ;, \t (tab), or `\ | ` (pipe) |
| First Row is Headers | boolean | true | Whether the first row contains column names | |
| Infer Types | boolean | true | Auto-convert numbers and booleans (disable for all-string output) | |
| Trim Whitespace | boolean | true | Remove leading/trailing whitespace from values | |
| Skip Empty Lines | boolean | true | Ignore blank rows in the CSV input |
Use Cases
Data Import
- Spreadsheet data: Convert exported CSV from Excel or Google Sheets into JSON
- Database exports: Parse database dump CSV files for processing
- Log files: Parse tab-delimited log files into structured objects
Format Conversion
- API preparation: Convert CSV data into JSON format for API requests
- Configuration files: Parse semicolon-delimited config files
- Data migration: Convert legacy CSV data to JSON for modern systems
Data Cleaning
- Type normalization: Use type inference to convert string numbers to actual numbers
- Whitespace cleanup: Automatically trim messy CSV data
- Empty row removal: Skip blank lines in poorly formatted CSV files
Configuration
| Name | Type | Default | Description |
|---|---|---|---|
| Delimiter | enum | , | Character used to separate fields in the CSV , ; \t | |
| First Row is Headers | boolean | true | Whether the first row contains column names |
| Infer Types | boolean | true | Auto-convert numbers and booleans (disable for all-string output) |
| Trim Whitespace | boolean | true | Remove leading/trailing whitespace from values |
| Skip Empty Lines | boolean | true | Ignore blank rows in the CSV input |
Examples
Parse this CSV text into JSON rows.1[2 {3 "name": "Alice",4 "age": 30,5 "city": "New York"6 },7 {8 "name": "Bob",9 "age": 25,10 "city": "London"11 }12]API Usage
curl -X POST https://your-domain.com/api/v1/utilities/convert.csv-to-json \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"inputs":{"primary":"name,age,city\nAlice,30,New York\nBob,25,London"},"config":{"delimiter":",","hasHeaders":true,"inferTypes":true,"trimWhitespace":true,"skipEmptyLines":true}}'1[2 {3 "name": "Alice",4 "age": 30,5 "city": "New York"6 },7 {8 "name": "Bob",9 "age": 25,10 "city": "London"11 }12]