A tiny, efficient, and cross-platform JSON to CSV converter.
npm install @alwatr/json2csvA tiny, efficient, and cross-platform JSON to CSV converter.
- Lightweight: Minimal footprint, zero dependencies (except type helpers).
- Fast: Optimized for performance using array mapping and joining.
- Flexible: Custom delimiters, optional headers, and value replacement support.
- Robust: Handles escaped characters, nested objects, and arrays correctly.
- Type-Safe: Written in TypeScript with full type definitions.
``bash`
yarn add @alwatr/json2csv
`typescript
import { jsonToCsv } from '@alwatr/json2csv';
const data = [
{ name: 'Ali', age: 30, city: 'Tehran' },
{ name: 'John', age: 25, city: 'New York' }
];
const csv = jsonToCsv(data);
console.log(csv);
/*
name,age,city
Ali,30,Tehran
John,25,New York
*/
`
You can customize the output using optional parameters:
`typescript`
const csv = jsonToCsv(
data, // Data array
';', // Custom delimiter (default: ',')
false // Include headers (default: true)
);
Converts a JSON array of objects to a CSV string.
- jsonData: Array
Returns: string` - The generated CSV string.
MPL-2.0