CSV ↔ JSON Converter
Convert CSV to JSON or JSON to CSV. Handles quoted fields, commas inside values, and optional headers.
CSV and JSON conversion in .NET backends
In my .NET projects, I need CSV/JSON conversion in two recurring situations: exporting EF Core query results to a file download for non-technical stakeholders who want to open the data in Excel, and importing bulk data from a CSV that an operations team uploads into an API endpoint that expects JSON. Getting the conversion right matters — CSV's edge cases (quoted fields, embedded commas, multi-line values) are subtle enough that hand-rolled parsers silently produce incorrect data on real-world input.
How to use this tool
- Select the direction — CSV → JSON or JSON → CSV — using the toggle.
- Paste your data into the input editor.
- Click Convert.
- Copy the result from the output panel.
CSV/JSON in .NET: the right library for each scenario
Do not write a CSV parser by hand. The split-on-comma approach fails on the first field that contains a comma inside quotes. In .NET, use the built-in System.Formats.Asn1... actually, there is no built-in CSV parser in the BCL. The two correct choices are:
- CsvHelper (NuGet:
CsvHelper) — the most widely used .NET CSV library. Maps CSV columns directly to C# POCOs via class maps or attribute-based configuration. Handles RFC 4180 edge cases (quoted fields, embedded quotes, multi-line values, BOM) correctly. - Microsoft.VisualBasic.FileIO.TextFieldParser — a built-in BCL type in the
Microsoft.VisualBasicassembly. Available on .NET without a NuGet package, handles most RFC 4180 cases, but has a less ergonomic API and no POCO mapping.
// Read a CSV upload in an ASP.NET Core endpoint and convert to DTOs with CsvHelper
app.MapPost("/import/products", async (IFormFile file) =>
{
using var reader = new StreamReader(file.OpenReadStream());
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<ProductDto>().ToList();
// records is now a List<ProductDto> — field names matched by header row
return Results.Ok(new { Imported = records.Count });
});
// Export EF Core results to CSV for a file download
app.MapGet("/export/orders", async (AppDbContext db) =>
{
var orders = await db.Orders.Select(o => new
{
o.Id, o.CustomerName, o.Total, o.CreatedAt
}).ToListAsync();
using var ms = new MemoryStream();
using var writer = new StreamWriter(ms);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(orders);
await writer.FlushAsync();
return Results.File(ms.ToArray(), "text/csv", "orders.csv");
});
How this tool converts CSV to JSON
Parses each row of the CSV into a JSON object. When First row is header is checked, the first line provides the property names for every object. Without headers, columns are named col1, col2, and so on. The result is a JSON array of objects — the standard format expected by REST APIs and most data processing tools.
How this tool converts JSON to CSV
Flattens a JSON array of objects into CSV rows. The property names of the first object become the column headers. Nested objects and arrays are not supported — they cause the conversion to fail with an error rather than being silently flattened or stringified. Flatten any nested structure into plain string/number fields before converting. Note: JSON distinguishes number types, booleans, and nulls; CSV treats everything as text. Type information is lost during JSON → CSV conversion.
Five real .NET use cases
- Exporting EF Core query results as a CSV file download in an ASP.NET Core endpoint. Use CsvHelper's
CsvWriter.WriteRecords()to stream anonymous type results directly from a LINQ query to aMemoryStream, then returnResults.File(). - Importing a bulk upload CSV from an operations team into an API that expects JSON. Convert the CSV to JSON here, inspect the structure, then write the
CsvHelperPOCO map to match the actual headers before wiring up the endpoint. - Turning an Azure SQL export (which produces CSV) into a JSON payload for a downstream microservice. Paste the CSV output here, convert to JSON, verify the field names and types match what the downstream API expects before writing the transformation code.
- Validating a CsvHelper class map by comparing the converted output against the expected DTO structure. Convert a sample row from the CSV to JSON here to see the exact field names and values — then confirm your
CsvClassMap<T>or property name attributes match. - Preparing seed data for an EF Core
HasData()call. If your seed data lives in a CSV (common when it comes from a business team), convert to JSON here and format it in the JSON Formatter before writing the C# object initializers.
Frequently asked questions
Why doesn't the BCL have a built-in CSV parser? CSV is not fully standardized — RFC 4180 is informational, not a formal standard, and different tools (Excel, Google Sheets, database export tools) produce slightly different CSV dialects. The BCL avoids including parsers for formats without a clear normative specification. Use CsvHelper for production .NET CSV work.
How do I handle a CSV where some columns have numbers and others have text in .NET? CsvHelper's POCO mapping reads each CSV column as a string, then applies type converters to convert to the target property type. If a field contains an empty string where your property is int, CsvHelper throws a CsvHelperException. Use nullable types (int?) for optional numeric columns.
Why does my CSV export from Excel fail to parse with Windows line endings? Excel's CSV export uses CRLF (\r\n) line endings. If your server-side parser splits on \n only, it leaves trailing \r characters on every value in the last column. Use CsvReader which handles CRLF automatically, or normalize line endings with text.Replace("\r\n", "\n") before parsing.
CSV edge cases that break manual parsers (RFC 4180)
- Commas in values: A field containing a comma must be enclosed in double quotes:
"Smith, John" - Quotes in values: A double quote inside a quoted field is escaped by doubling it:
"He said ""hello""" - Newlines in values: A field can contain a newline if the entire field is quoted — this produces a multi-line CSV cell
- Line endings: RFC 4180 specifies CRLF (
\r\n) as the line ending, but most tools accept LF-only files. Mixed line endings in a single file will cause subtle row count discrepancies. - Encoding: CSV has no encoding declaration — UTF-8 with BOM is the most widely compatible choice for Excel compatibility
CSV input
name,age,city Alice,30,London Bob,25,Paris Carol,35,Berlin
Converted output
JSON result