jsontocsv
A simple TypeScript/JavaScript utility to convert an array of JSON objects into a downloadable CSV file in the browser.
Features
- Accepts only arrays of objects (JSON format)
- Escapes quotes and handles special characters
- Triggers a CSV file download in the browser
Where to Use
- Web applications: Use in any browser-based project (React, Vue, Angular, plain JS/TS, etc.) where you need to let users export data as CSV.
- Dashboards: Allow users to download reports or tables as CSV files.
- Admin panels: Export filtered or tabular data for offline analysis.
Note: This package is intended for use in browser environments. It will not work in Node.js or server-side code, as it relies on browser APIs (document
, Blob
, URL
).
Installation
npm install csvdrop
Usage
import { jsontocsv } from "csvdrop";
const data = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
];
jsontocsv(data, "mydata.csv");
API
jsontocsv(data, filename?)
data
: Array of objects (required)
filename
: Name for the downloaded CSV file (default: data.csv
)
Example
const data = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
];
jsontocsv(data, "users.csv");
Notes
- This function must be run in a browser environment (uses
document
, Blob
, and URL
).
- Throws an error if the input is not a non-empty array of objects.
License
MIT