CSV to JSON Tutorial: Convert Rows to JSON Objects

Last updated:

CSV to JSON conversion maps each CSV row to a JSON object and wraps all rows in an array — a 100-row, 3-column CSV produces 100 objects each with 3 keys. The first row becomes property names; each subsequent row becomes 1 object. Papa Parse handles the 3 hardest edge cases automatically: quoted commas inside fields, multi-line quoted cells, and escaped double quotes. Python's built-in csv.DictReader needs 0 external packages. This guide covers basic conversion, quoted field handling, type coercion (numeric strings to numbers, empty strings to null), header mismatches across rows, JavaScript with Papa Parse, Python with csv and json modules, and command-line conversion with jq.

Basic CSV to JSON conversion

With headers in the first row, conversion is straightforward.

id,name,email,active
1,Alice,alice@example.com,true
2,Bob,bob@example.com,false
[
  {
    "id": 1,
    "name": "Alice",
    "email": "alice@example.com",
    "active": true
  },
  {
    "id": 2,
    "name": "Bob",
    "email": "bob@example.com",
    "active": false
  }
]

Handle quoted fields correctly

Proper CSV parsers preserve commas and line breaks inside quoted fields. Splitting each line with line.split(',') is not enough for real CSV.

id,note
1,"Hello, world"
2,"Line one
Line two"
3,"She said ""yes"""

Decide how to handle types

CSV has no native JSON types. Every cell starts as text. A converter may coerce numbers and booleans, but you should verify that the result matches what your app expects.

// CSV values
"42", "true", "", "00123"

// Possible JSON interpretations
42, true, "", "00123"

Be careful with IDs, postal codes, and account numbers. They may look numeric but should often stay strings.

Clean header names

Headers become JSON keys. Trim spaces and standardize naming before using the output in code.

First Name,Email Address,Signup Date
Alice,alice@example.com,2024-01-15

// Better JSON keys after cleanup:
// firstName, emailAddress, signupDate

Common CSV to JSON mistakes

  • Using a naive comma split that breaks quoted fields.
  • Assuming all numeric-looking values should be numbers.
  • Leaving duplicate or empty headers unresolved.
  • Forgetting to validate required fields after conversion.
  • Expecting CSV to preserve nested JSON structure automatically.

Convert CSV to JSON in the browser

Use Jsonic's CSV to JSON converter to parse quoted fields, coerce common types, copy output, or download the result as JSON.

Open CSV to JSON

Frequently asked questions

How do I handle CSV headers in JSON output?

The header row becomes JSON object keys. Normalize headers with spaces or special characters (e.g., "First Name" to "firstName"). For header-less CSV, supply column names manually or the parser will use positional keys. Always trim whitespace from headers.

What JSON type should CSV numbers become?

Dynamic typing converts "42" to 42 and "3.14" to 3.14 — good for measurements and counts. Keep ZIP codes, phone numbers, account numbers, and ISBNs as strings to avoid losing leading zeros. Use dtype=str in pandas or a transform callback in PapaParse for sensitive columns.

How do I handle CSV files with no header row?

Supply column names externally using the fieldnames parameter (Python csv.DictReader) or equivalent in your library. Alternatively use positional keys ("col1", "col2"). Named columns produce more useful JSON for downstream consumers.

What happens with empty CSV cells in JSON?

Converters vary — empty cells may become null, empty string "", or be omitted. Choose based on your use case: null for "no value", "" for "empty text", or omit if absence equals null in your application. Configure explicitly; Python csv.DictReader returns empty strings by default.

How do I convert a CSV file to JSON in Python?

Use csv.DictReader and json.dumps: import csv, json; rows = list(csv.DictReader(open("data.csv"))); print(json.dumps(rows, indent=2)). For automatic type coercion use pandas: pd.read_csv("data.csv").to_json(orient="records", indent=2). Specify dtype=str for ID and code columns.

Can CSV multiline cells be represented in JSON?

Yes. A proper CSV parser returns the multiline string (with literal newlines) as the cell value. In JSON output, those newlines become the \n escape sequence, producing a valid JSON string. Double-quoted CSV cells with newlines inside are handled correctly by any RFC 4180-compliant parser.