Converting JSON to CSV is one of the most common data-shuffling tasks in any analyst, data engineer, or full-stack developer's day. APIs return JSON; spreadsheets, BI tools, and most ETL pipelines speak CSV. This converter takes a JSON array of flat objects and produces a CSV file with the object keys as headers and each object as a row — ready to open in Excel, Google Sheets, Numbers, or import into PostgreSQL via COPY or MySQL via LOAD DATA INFILE.
How JSON to CSV Conversion Works
The converter expects a JSON array of objects, like [{"name":"Alice","age":30},{"name":"Bob","age":25}]. It extracts the keys from the first object to build the CSV header row, then iterates through each object and emits a row of values. Values containing commas, quotes, or newlines are automatically wrapped in double quotes with internal quotes escaped (RFC 4180 compliant). The output uses \n line endings; if you need Windows-style \r\n, your tool will usually convert on save.
When to Convert JSON to CSV
- Spreadsheet analysis — open an API response in Google Sheets or Excel for pivot tables.
- BI tool ingestion — Tableau, Power BI, Looker, Metabase — all happiest with CSV.
- Database bulk import — PostgreSQL
COPY, MySQL LOAD DATA, BigQuery load jobs are all CSV-first. - Stakeholder reports — non-technical colleagues open CSV in Excel without a second thought.
- CRM / email-tool imports — Mailchimp, HubSpot, Salesforce all import contact lists as CSV.
JSON to CSV in Code
function jsonToCSV(arr) {
if (!Array.isArray(arr) || !arr.length) return '';
const keys = Object.keys(arr[0]);
const escape = v => {
const s = String(v ?? '');
return /[",\n]/.test(s) ? '"' + s.replace(/"/g, '""') + '"' : s;
};
return [
keys.join(','),
...arr.map(o => keys.map(k => escape(o[k])).join(','))
].join('\n');
}
For nested JSON, flatten first with lodash.flatten or a custom dot-notation flattener. For the reverse direction, use our CSV to JSON converter.
Handling Nested JSON
CSV is inherently flat — it has no native way to express nested objects or arrays. If your JSON has structure like {"user": {"name": "Alice"}, "tags": ["a", "b"]}, you have three options: (1) flatten — emit columns like user.name and tags.0, tags.1; (2) JSON-in-cell — emit the nested value as a JSON string in a single cell; (3) explode — emit one row per array element, duplicating the parent fields. This tool uses option (2) for nested objects automatically; for option (1) or (3), pre-process your JSON first.