Skip to content

Guides

How to convert CSV to JSON (online, Excel, Python, JavaScript, PowerShell)

Turn a CSV file into JSON online, in Excel, Python, JavaScript or PowerShell. Which output shape to pick (array, JSON Lines), and the mistakes that quietly break your data.

By Ketan · Updated

A CSV file is a table written as text: one row per line, values separated by commas. JSON is how most web APIs, JavaScript apps and NoSQL databases want their data. Converting between them sounds trivial, and usually is, until a value contains a comma, a ZIP code loses its leading zero, or an accented name turns into gibberish.

I’ve done this conversion more times than I can count, usually when a spreadsheet needs to go into an API, a test fixture or a NoSQL collection. It’s almost always quick. When it isn’t, the cause is nearly always one of the five problems at the end of this guide.

This guide covers the common ways to do it (online, Excel, Python, JavaScript, PowerShell and Power Automate) and the handful of details that decide whether the result is correct.

The quick way: in your browser

If you just need the file converted, use the CSV to JSON converter. Drop the file in, choose an output shape, and download. It runs locally in the page, so it’s fine for files with customer or financial data.

Picking the right JSON shape

The same CSV can become JSON in a few ways. Given this file:

name,city,orders
Asha,Pune,3
Tom,Berlin,1

Array of objects is the most common. Each row becomes an object, and the header row provides the keys:

[
  { "name": "Asha", "city": "Pune", "orders": 3 },
  { "name": "Tom", "city": "Berlin", "orders": 1 }
]

Use it when the JSON is going into an API, a JavaScript app, or MongoDB.

Array of arrays keeps the header as the first row and drops the repeated keys. It’s smaller, and charting libraries and spreadsheet APIs (Google Sheets, for one) often want it:

[["name", "city", "orders"], ["Asha", "Pune", 3], ["Tom", "Berlin", 1]]

JSON Lines (.jsonl) is one object per line, with no surrounding brackets. It’s the format to pick for large files going into BigQuery, DuckDB, Elasticsearch or a log pipeline, because tools can read it one line at a time instead of loading the whole file.

In Excel

Excel can’t save JSON directly. The usual workaround is Power Query in reverse: load the JSON into Excel, not out of it. To go from Excel to JSON you have two practical options:

  1. Save as CSV, then convert. Go to File → Save As → CSV UTF-8, then use the converter above. It takes under a minute and handles quoting correctly.
  2. Office Scripts (Excel on the web, business plans). A short script can read the used range and build JSON. It’s worth it if you repeat the job every week, but overkill for a one-off.

Avoid building JSON with a formula like ="{""name"":"""&A2&"""}". It works on clean data and breaks the moment a cell contains a quote mark or a line break.

In Python

The standard library is enough:

import csv, json

with open("customers.csv", newline="", encoding="utf-8") as f:
    rows = list(csv.DictReader(f))

with open("customers.json", "w", encoding="utf-8") as f:
    json.dump(rows, f, indent=2, ensure_ascii=False)

DictReader gives you an array of objects. Every value comes out as text, which is usually what you want. Convert specific columns yourself (int(row["orders"])) rather than guessing.

With pandas it’s one line, and you get type detection for free:

import pandas as pd
pd.read_csv("customers.csv").to_json("customers.json", orient="records", indent=2)

Use orient="records" for an array of objects, or orient="records", lines=True for JSON Lines.

In JavaScript

In the browser, use a real CSV parser rather than split(","), which breaks on quoted commas. Papa Parse is the usual choice:

import Papa from "papaparse";

Papa.parse(file, {                 // a File from <input type="file">
  header: true,                    // use the first row as keys
  dynamicTyping: true,             // "42" -> 42, "true" -> true
  skipEmptyLines: true,
  complete: (result) => {
    const json = JSON.stringify(result.data, null, 2);
    console.log(json);
  },
});

In Node.js, the csv-parse package does the same:

import { readFileSync, writeFileSync } from "node:fs";
import { parse } from "csv-parse/sync";

const rows = parse(readFileSync("customers.csv"), { columns: true, skip_empty_lines: true });
writeFileSync("customers.json", JSON.stringify(rows, null, 2));

columns: true gives an array of objects, and values stay text unless you add a cast option.

In PowerShell

PowerShell can do it in one line, with nothing to install:

Import-Csv customers.csv | ConvertTo-Json | Set-Content customers.json -Encoding utf8

Three details worth knowing:

  • Every value comes out as text, because Import-Csv doesn’t detect numbers.
  • A file with one data row becomes a single object, not an array, in Windows PowerShell 5.1. To always get an array, use ConvertTo-Json -InputObject @(Import-Csv customers.csv), or ConvertTo-Json -AsArray in PowerShell 7.
  • Semicolon-separated files need Import-Csv customers.csv -Delimiter ';'.

In Power Automate

Power Automate has no built-in “CSV to JSON” action. The two reliable approaches:

  1. Turn the file into an Excel table. Save it as an .xlsx in OneDrive or SharePoint, format the data as a table, and use the Excel Online action List rows present in a table. Its output is already a JSON array of objects, one per row.
  2. Parse the text yourself. Get the file content, split it into lines with split(body, decodeUriComponent('%0A')), then use a Select action to map each line’s columns, splitting on commas, to named fields. This works for simple files, but breaks on values with commas inside quotes, so prefer option 1 when the data comes from people.

For a one-off file, converting it in the browser and attaching the JSON is far quicker than building a flow.

JSON Lines (.jsonl) for large files

JSON Lines puts one JSON object on each line, with no surrounding brackets or commas:

{"name": "Asha", "city": "Pune"}
{"name": "Tom", "city": "Berlin"}

It’s the better choice for large files and data pipelines. Tools can read and write it one line at a time, a damaged line doesn’t break the whole file, and you can append new records without rewriting it. BigQuery, Elasticsearch, DuckDB and most log tools load it directly.

  • Online: choose One object per line (JSON Lines) in the CSV to JSON converter. Files over 20 MB are converted with DuckDB and streamed straight to the download.
  • Python: pd.read_csv("big.csv").to_json("big.jsonl", orient="records", lines=True)
  • DuckDB: COPY (SELECT * FROM 'big.csv') TO 'big.jsonl' (FORMAT JSON);

Five things that go wrong

From experience: leading zeros do the most damage, because nothing fails. The JSON looks fine, the API accepts it, and the wrong postcodes only surface much later. I treat every ID-like column as text by default and only convert the columns I know are quantities.

1. Leading zeros vanish. Type detection turns 02134 into 2134. Phone numbers, ZIP and PIN codes, product codes and account numbers should stay text. In the converter, untick Detect numbers. In pandas, pass dtype={"zip": str}.

2. Commas inside values split a column in two. Stapler, heavy duty must be written as "Stapler, heavy duty" in the CSV. Exports from Excel and Google Sheets do this automatically, but hand-made or system-generated files often don’t. If your row counts or column counts look off, this is usually why.

3. Garbled characters. If José comes out as José, the CSV was saved in an older encoding such as Windows-1252. Re-save it as CSV UTF-8 from Excel, or open it in Python with encoding="cp1252".

4. Semicolons instead of commas. In much of Europe, Excel saves “CSV” files with semicolons. A converter that assumes commas will give you one giant column. Good tools detect the separator. In Python, pass delimiter=";".

5. Empty strings versus missing values. An empty cell can become "", null, or be left out entirely, depending on the tool. Decide which one the system receiving the JSON expects, and check a few rows before you send the whole file.

Going the other way

To turn JSON back into CSV, flip the converter to JSON → CSV. If the JSON is nested, for example {"address": {"city": "Pune"}}, use Flatten JSON instead. It spreads nested fields into their own columns, like address.city, so the result opens cleanly in Excel.