@@ -20,6 +20,8 @@ Convert JSON records to clean CSV with TypeScript-first options.
2020npm install json-csv-kit
2121```
2222
23+ ` json-csv-kit ` is ESM-only and targets Node.js 20 or newer. It also works in browsers through modern bundlers such as Vite, Rollup, webpack and esbuild.
24+
2325## Quick Start
2426
2527``` ts
@@ -58,6 +60,8 @@ customer.name,customer.region,total
5860Northwind,EU,120
5961```
6062
63+ Column order is based on the first time each key is discovered while reading your records. Use ` columns ` when you need a stable public export format.
64+
6165## Which API should I use?
6266
6367| Function | Use it when you need |
@@ -112,6 +116,70 @@ jsonToCsv(rows, {
112116
113117The generic API accepts normal TypeScript interfaces; your row type does not need an index signature.
114118
119+ ``` ts
120+ interface Order {
121+ customer: {
122+ name: string ;
123+ };
124+ totalCents: number ;
125+ }
126+
127+ const orders: Order [] = [
128+ { customer: { name: ' Ada' }, totalCents: 1234 }
129+ ];
130+
131+ const csv = jsonToCsv (orders , {
132+ columns: [
133+ { key: ' customer' , path: ' customer.name' },
134+ {
135+ key: ' total' ,
136+ accessor : (row ) => row .totalCents ,
137+ formatter : (value ) => ` $${Number (value ) / 100 } `
138+ }
139+ ]
140+ });
141+ ```
142+
143+ ## Browser download
144+
145+ The package returns a string, so you can decide how to save or upload it.
146+
147+ ``` ts
148+ import { jsonToCsv } from ' json-csv-kit' ;
149+
150+ const csv = jsonToCsv (rows );
151+ const blob = new Blob ([csv ], { type: ' text/csv;charset=utf-8' });
152+ const url = URL .createObjectURL (blob );
153+
154+ const link = document .createElement (' a' );
155+ link .href = url ;
156+ link .download = ' export.csv' ;
157+ link .click ();
158+
159+ URL .revokeObjectURL (url );
160+ ```
161+
162+ ## Nested data and paths
163+
164+ With inferred columns, nested plain objects are flattened into dot-path keys. Arrays, dates and other non-plain values stay as values.
165+
166+ ``` ts
167+ jsonToCsv ([{ user: { name: ' Ada' }, tags: [' admin' , ' ops' ] }]);
168+ ```
169+
170+ ``` csv
171+ user.name,tags
172+ Ada,"[""admin"",""ops""]"
173+ ```
174+
175+ If a source object has a real key containing dots, direct keys are checked before dot-path traversal:
176+
177+ ``` ts
178+ jsonToCsv ([{ ' user.name' : ' Ada' }]);
179+ ```
180+
181+ Use ` accessor ` when your source needs bracket notation, computed values or more control than simple dot paths.
182+
115183## CSV safety
116184
117185Values are escaped according to normal CSV rules:
@@ -177,6 +245,33 @@ interface JsonToCsvOptions<TRecord> {
177245| ` escapeFormulae ` | ` false ` | prefix spreadsheet-like formulas |
178246| ` dateFormatter ` | ISO string | format ` Date ` values |
179247
248+ ## Arrays, dates and null values
249+
250+ Arrays are serialized as JSON by default because that preserves the original data most safely:
251+
252+ ``` ts
253+ jsonToCsv ([{ tags: [' admin' , ' ops' ] }]);
254+ ```
255+
256+ Use ` arrayMode: 'join' ` for human-readable lists, or ` arrayMode: 'empty' ` when arrays should be skipped:
257+
258+ ``` ts
259+ jsonToCsv ([{ tags: [' admin' , ' ops' ] }], {
260+ arrayMode: ' join' ,
261+ arraySeparator: ' | '
262+ });
263+ ```
264+
265+ ` null ` and ` undefined ` are exported as an empty string by default. Use ` nullValue ` when your downstream tool needs an explicit marker:
266+
267+ ``` ts
268+ jsonToCsv ([{ name: ' Ada' , team: null }], {
269+ nullValue: ' NULL'
270+ });
271+ ```
272+
273+ Dates use ` toISOString() ` by default. Use ` dateFormatter ` for another format.
274+
180275## Ecosystem recipes
181276
182277Use with ` object-key-paths ` to inspect columns before exporting:
@@ -223,7 +318,7 @@ const csv = jsonToCsv(rows);
223318## Notes
224319
225320- Input must be an array of plain objects.
226- - Dot paths are intentionally simple. Use ` accessor ` or ` object-path-kit ` for bracket notation and keys containing dots .
321+ - Dot paths are intentionally simple. Use ` accessor ` or ` object-path-kit ` for bracket notation, ambiguous paths or computed values .
227322- Arrays are serialized as JSON by default so no information is lost.
228323- ` BigInt ` values inside arrays or objects are converted to strings during JSON serialization.
229324- Circular references are represented as ` [Circular] ` instead of crashing the export.
0 commit comments