Development6 min read

Working with CSV and JSON: Converting Between Data Formats

Learn how to convert between CSV and JSON formats. Covers practical use cases, handling nested data, common pitfalls, and when to use each format.

Your product manager exports a spreadsheet as CSV and asks you to "just put it in the API." Your backend returns JSON and the analyst wants it "in Excel." These conversations happen weekly on every engineering team, and they always involve converting between CSV and JSON.

Both formats represent structured data, but they think about it differently. Understanding those differences makes the conversion smooth instead of surprising.

Two Philosophies of Data

CSV (Comma-Separated Values) is flat and tabular. It's a grid — rows and columns, like a spreadsheet. The first row is typically headers, and every subsequent row has the same number of fields.

name,email,role,department
Alice,alice@example.com,admin,Engineering
Bob,bob@example.com,editor,Marketing
Carol,carol@example.com,viewer,Sales

JSON (JavaScript Object Notation) is hierarchical. It supports nesting, arrays, multiple data types, and complex relationships between objects.

[
  {
    "name": "Alice",
    "email": "alice@example.com",
    "role": "admin",
    "department": "Engineering"
  }
]

For flat data like the example above, they're essentially interchangeable. The problems start when data isn't flat.

CSV to JSON: When Spreadsheets Meet APIs

The most common scenario: someone hands you a CSV file that needs to become JSON for an API import, a database seed, or a frontend data source.

With simple tabular data, the conversion is straightforward. Each row becomes a JSON object, each column header becomes a key:

product,price,stock
Widget,9.99,150
Gadget,24.99,42
Doohickey,4.99,500

Becomes:

[
  {"product": "Widget", "price": "9.99", "stock": "150"},
  {"product": "Gadget", "price": "24.99", "stock": "42"},
  {"product": "Doohickey", "price": "4.99", "stock": "500"}
]

Paste your CSV into the CSV to JSON converter and get clean JSON output immediately.

The Type Problem

Notice something in that output? Every value is a string. CSV has no concept of data types — 9.99, 150, true, and null are all just text. When you convert to JSON, you need to decide: should "150" stay a string or become the number 150?

For API consumption, you usually want typed values. For display purposes, strings are often fine. Be aware of the distinction and post-process if needed.

Delimiter Variations

"Comma-separated" is a lie about half the time. Depending on locale and source:

  • European Excel exports often use semicolons (;) because commas are decimal separators
  • Tab-separated files (TSV) are common in scientific data
  • Pipe-delimited (|) shows up in legacy systems

Most conversion tools handle standard comma separation. If your data uses a different delimiter, you may need to find-and-replace before converting.

JSON to CSV: Flattening the Hierarchy

Going from JSON to CSV means squeezing hierarchical data into a flat table. Simple arrays of objects convert cleanly, but nested structures require decisions.

Take this JSON:

[
  {
    "name": "Alice",
    "address": {
      "city": "Portland",
      "state": "OR"
    },
    "skills": ["Python", "SQL", "Docker"]
  }
]

How does address become columns? Common approaches:

Dot notation flattening:

name,address.city,address.state,skills
Alice,Portland,OR,"Python, SQL, Docker"

Serialized nesting:

name,address,skills
Alice,"{""city"":""Portland"",""state"":""OR""}","[""Python"",""SQL"",""Docker""]"

The first is more useful for analysis. The second preserves all the data. The JSON to CSV converter handles flat structures cleanly. For deeply nested JSON, you may need to flatten it first.

If your JSON has inconsistent keys across objects (some have a field, others don't), the CSV output will have empty cells for missing values. This is normal and expected — CSV must have the same columns for every row.

Real-World Conversion Scenarios

Importing Spreadsheet Data Into an App

Your operations team maintains a spreadsheet of product catalog data. They export it as CSV, you need to turn it into JSON for your REST API's bulk import endpoint.

Steps:

  1. Get the CSV export
  2. Convert to JSON using the CSV to JSON tool
  3. Verify the structure matches your API's expected format
  4. Format the JSON for readability using the JSON Formatter
  5. Adjust types or field names as needed, then POST it

Exporting API Data for Analysis

A data analyst needs records from your API in a spreadsheet. Your API returns paginated JSON. Copy the response, convert to CSV, and hand them a file they can open in Excel or Google Sheets.

Migration Between Systems

System A exports CSV. System B accepts JSON. This happens constantly with CRM imports, email list migrations, and database transfers. Automated pipelines handle this at scale, but for one-off migrations, a quick conversion tool is faster than writing a script.

Testing and Mock Data

Building test fixtures? Start with a CSV of test cases (easy to edit in a spreadsheet), convert to JSON for your test runner. Or go the other direction — take a JSON API response and convert it to CSV for a test data spreadsheet.

Handling Edge Cases

CSV and JSON handle special characters differently, and these edge cases cause the most confusion:

Commas in values. CSV handles this by quoting the field:

name,description
Widget,"Small, lightweight, durable"

Newlines in values. Also handled by quoting:

name,notes
Widget,"Line one
Line two"

Quotes in values. Escaped by doubling:

name,description
Widget,"The ""best"" widget"

Null vs. empty. JSON has explicit null. CSV has empty fields. When converting, empty CSV fields typically become empty strings in JSON, not null. This distinction matters if your API validates for null values.

Unicode. Both formats support Unicode, but some older tools choke on non-ASCII characters in CSV. If you see garbled characters after conversion, check the encoding — it should be UTF-8.

When to Use Which Format

| Scenario | Better Format | Why | |----------|--------------|-----| | API communication | JSON | Native types, nesting, universal support | | Spreadsheet analysis | CSV | Opens directly in Excel/Sheets | | Config files | JSON (or YAML) | Supports nesting and comments (YAML) | | Database export | CSV | Flat tabular data maps perfectly | | Log storage | JSON | Structured logging with metadata | | Human editing | CSV | Simpler syntax, easier to type | | Complex structures | JSON | Arrays, nested objects, mixed types |

Try It Yourself

Data format conversion doesn't need a script. For quick one-off conversions, these tools handle it instantly:

Everything runs in your browser. Your data never leaves your device.

Tools Mentioned

Related Articles