Basic CSV to JSON Array Conversion

Learn how to convert a simple CSV file to a JSON array. Step-by-step guide covering header parsing, row mapping, and output formatting for common use cases.

Basic Conversion

Detailed Explanation

Converting CSV to JSON Arrays

The most fundamental CSV-to-JSON conversion takes each row of a CSV file and turns it into a JSON object, using the header row as property keys.

Input CSV

name,age,city
Alice,30,New York
Bob,25,London
Charlie,35,Tokyo

Output JSON

[
  { "name": "Alice", "age": "30", "city": "New York" },
  { "name": "Bob", "age": "25", "city": "London" },
  { "name": "Charlie", "age": "35", "city": "Tokyo" }
]

How the conversion works

  1. Parse the header row. The first line of the CSV is split by the delimiter (comma by default) to produce an array of column names: ["name", "age", "city"].
  2. Iterate over data rows. Each subsequent line is split by the same delimiter to produce an array of values.
  3. Zip keys and values. For each data row, pair the column names with the corresponding values to create a JSON object.
  4. Collect into an array. All objects are gathered into a single JSON array.

Key considerations

  • All values start as strings. CSV has no type system, so "30" is a string, not a number. You may need a post-processing step to coerce types (see the Number vs String Type Inference example).
  • Empty trailing lines in the CSV can produce empty objects. Always trim your input before parsing.
  • Column count mismatches occur when a data row has more or fewer fields than the header. Robust parsers either pad with null or truncate extra fields.

This basic pattern is the foundation for every other CSV-to-JSON conversion approach. Once you understand header mapping, you can extend it with nested structures, custom delimiters, and type inference.

Use Case

Importing a customer list exported from a spreadsheet into a REST API that expects JSON payloads. Each row becomes a JSON object ready for a POST request body.

Try It — CSV ↔ JSON Converter

Open full tool