FmtDev
Language
Back to blog
March 25, 2026

How to Convert CSV to JSON: A Complete Guide for Developers

Learn how to convert CSV files to JSON format. Understand the structure differences, common pitfalls, and how to handle headers, nested data, and special characters correctly.

Why Convert CSV to JSON?

CSV and JSON are two of the most common data formats in software development. But they serve very different purposes.

CSV (Comma-Separated Values) is flat, tabular data — rows and columns, like a spreadsheet. It is simple, compact, and universally supported by Excel, Google Sheets, databases, and data analysis tools.

JSON (JavaScript Object Notation) is structured, hierarchical data — objects with keys and values, arrays, and nested structures. It is the standard format for APIs, configuration files, and modern web applications.

At some point, every developer needs to convert between them. Maybe you exported data from a database as CSV and need to send it to an API that expects JSON. Maybe you received a spreadsheet from a client and need to process it programmatically.

This guide covers everything you need to know about converting CSV to JSON correctly.

The Basic Conversion

A simple CSV file:

name,age,city
Alice,30,Paris
Bob,25,London
Charlie,35,Berlin

Converts to this JSON:

[
  { "name": "Alice", "age": "30", "city": "Paris" },
  { "name": "Bob", "age": "25", "city": "London" },
  { "name": "Charlie", "age": "35", "city": "Berlin" }
]

The first row becomes the keys (property names). Each subsequent row becomes an object in a JSON array.

This looks straightforward, but real-world CSV files are rarely this clean.

Common Pitfalls

1. Everything Is a String

Notice that "age": "30" is a string, not a number. CSV has no type system. Every value is text. When converting to JSON, you need to decide:

  • Should "30" become the number 30?
  • Should "true" become the boolean true?
  • Should "" become null?

A naive converter keeps everything as strings. A smart converter detects and converts types automatically.

2. Commas Inside Values

What happens when a value contains a comma?

name,description,city
Alice,"Loves coding, hiking",Paris

The value Loves coding, hiking is wrapped in quotes to prevent the comma from being treated as a delimiter. Your parser must handle quoted fields correctly, or it will split this into the wrong number of columns.

3. Line Breaks Inside Values

CSV allows line breaks inside quoted fields:

name,bio
Alice,"Software engineer.
Loves open source."

This is valid CSV. The bio spans two lines but is one field. Many simple parsers break on this.

4. Different Delimiters

Not all "CSV" files use commas. Common alternatives:

| Delimiter | Name | Common In | |---|---|---| | , | Comma | US, UK, most software | | ; | Semicolon | France, Germany, Excel EU | | \t | Tab | TSV files, database exports | | \| | Pipe | Legacy systems |

If you open a European Excel export expecting commas, you will get garbage output. Always check the actual delimiter before parsing.

5. Encoding Issues

CSV files can be encoded in:

  • UTF-8 (modern standard)
  • UTF-8 with BOM (Windows Excel default)
  • Latin-1 / ISO-8859-1 (older European files)
  • Windows-1252 (legacy Windows)

If your parser assumes UTF-8 but the file is Latin-1, special characters like é, ñ, or ü will be corrupted.

6. Missing or Extra Columns

Real CSV files often have:

  • rows with fewer columns than the header
  • rows with more columns than the header
  • completely empty rows
  • trailing commas creating ghost columns

Your converter needs to handle all of these gracefully.

Converting CSV to JSON in JavaScript

Here is a basic converter:

function csvToJson(csv) {
  const lines = csv.trim().split('\n');
  const headers = lines[0].split(',');
  
  return lines.slice(1).map(line => {
    const values = line.split(',');
    const obj = {};
    headers.forEach((header, i) => {
      obj[header.trim()] = values[i]?.trim() || '';
    });
    return obj;
  });
}

This works for simple CSV but breaks on:

  • quoted fields with commas
  • line breaks inside values
  • semicolon delimiters
  • encoding issues

For production use, use a proper parsing library or a tool that handles these edge cases.

Converting CSV to JSON in Python

import csv
import json

with open('data.csv', 'r') as f:
    reader = csv.DictReader(f)
    data = list(reader)

with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)

Python's built-in csv.DictReader handles quoted fields, different delimiters, and most edge cases correctly. It is the recommended approach for Python developers.

JSON to CSV (The Reverse)

Converting JSON back to CSV is trickier because JSON can be nested:

{
  "name": "Alice",
  "address": {
    "city": "Paris",
    "country": "France"
  }
}

How do you flatten address.city and address.country into CSV columns? Common approaches:

  • Dot notation: columns become address.city, address.country
  • Flattening: pull nested values up to the top level
  • Ignoring: skip nested objects entirely

There is no universal standard. Your approach depends on your use case.

When to Use CSV vs JSON

| Use Case | Best Format | |---|---| | Spreadsheet data | CSV | | API requests and responses | JSON | | Database exports for analysis | CSV | | Configuration files | JSON | | Sharing data with non-developers | CSV | | Sharing data between services | JSON | | Large datasets (millions of rows) | CSV (smaller file size) | | Complex nested structures | JSON |

Converting Safely

When converting sensitive data (customer records, financial data, internal business data), be careful about where the conversion happens.

Many online CSV-to-JSON converters process your data on their servers. You upload a file, their server parses it, and sends back the result. Your data has now been transmitted to and processed by a third party.

If your CSV contains any sensitive information — names, emails, financial figures, internal business data — use a converter that runs entirely in your browser. No data should leave your machine.

The FmtDev CSV to JSON Converter processes everything locally. Your data never leaves your browser.

Key Takeaways

  1. CSV is flat, JSON is structured — conversion requires mapping columns to keys
  2. Watch for commas inside values, encoding issues, and different delimiters
  3. All CSV values are strings — decide how to handle type conversion
  4. Nested JSON cannot be directly represented in CSV without flattening
  5. Always check the delimiter and encoding before parsing
  6. For sensitive data, convert locally — never upload to online tools

Related Articles

Related Tool

Ready to use the Offline JSON Formatter & Beautifier (No Server Logs) tool? All execution is 100% local.

Open Offline JSON Formatter & Beautifier (No Server Logs)