|
| 1 | +# json-csv-kit |
| 2 | + |
| 3 | +[](https://github.com/Recoveredd/json-csv-kit/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +Convert JSON records to clean CSV with TypeScript-first options. |
| 6 | + |
| 7 | +`json-csv-kit` is a small utility for exports, admin tools, reports, support dashboards, docs generators and browser-based data tools. It keeps the common JSON-to-CSV path simple while leaving room for explicit columns, nested data and safe CSV escaping. |
| 8 | + |
| 9 | +## Package quality |
| 10 | + |
| 11 | +- TypeScript types are generated from the source. |
| 12 | +- ESM-only package with no runtime dependencies. |
| 13 | +- Marked as side-effect free for bundlers. |
| 14 | +- Tested on Node.js 20 and 22 with GitHub Actions. |
| 15 | +- Works in Node.js, browsers, Vite apps and static docs tooling. |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install json-csv-kit |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +```ts |
| 26 | +import { jsonToCsv } from 'json-csv-kit'; |
| 27 | + |
| 28 | +const csv = jsonToCsv([ |
| 29 | + { name: 'Ada', role: 'Engineer' }, |
| 30 | + { name: 'Grace', role: 'Admiral' } |
| 31 | +]); |
| 32 | + |
| 33 | +console.log(csv); |
| 34 | +``` |
| 35 | + |
| 36 | +```csv |
| 37 | +name,role |
| 38 | +Ada,Engineer |
| 39 | +Grace,Admiral |
| 40 | +``` |
| 41 | + |
| 42 | +Nested plain objects are flattened by default: |
| 43 | + |
| 44 | +```ts |
| 45 | +jsonToCsv([ |
| 46 | + { |
| 47 | + customer: { |
| 48 | + name: 'Northwind', |
| 49 | + region: 'EU' |
| 50 | + }, |
| 51 | + total: 120 |
| 52 | + } |
| 53 | +]); |
| 54 | +``` |
| 55 | + |
| 56 | +```csv |
| 57 | +customer.name,customer.region,total |
| 58 | +Northwind,EU,120 |
| 59 | +``` |
| 60 | + |
| 61 | +## Which API should I use? |
| 62 | + |
| 63 | +| Function | Use it when you need | |
| 64 | +| --- | --- | |
| 65 | +| `jsonToCsv` | convert an array of records to a CSV string | |
| 66 | +| `toCsv` | short alias for `jsonToCsv` | |
| 67 | +| `inferCsvColumns` | inspect the columns that would be generated | |
| 68 | +| `flattenRecord` | flatten one nested object into dot-path keys | |
| 69 | +| `escapeCsvCell` | escape a single cell before composing CSV yourself | |
| 70 | + |
| 71 | +## Explicit columns |
| 72 | + |
| 73 | +Use explicit columns when you need stable order, custom headers or a subset of fields. |
| 74 | + |
| 75 | +```ts |
| 76 | +jsonToCsv(rows, { |
| 77 | + columns: [ |
| 78 | + { key: 'customer', header: 'Customer', path: 'customer.name' }, |
| 79 | + { key: 'region', header: 'Region', path: 'customer.region' }, |
| 80 | + { key: 'total', header: 'Total' } |
| 81 | + ] |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +Use an accessor for custom logic or when paths need bracket notation: |
| 86 | + |
| 87 | +```ts |
| 88 | +jsonToCsv(rows, { |
| 89 | + columns: [ |
| 90 | + { |
| 91 | + key: 'city', |
| 92 | + header: 'City', |
| 93 | + accessor: (row) => row.customer?.['billing.address']?.city |
| 94 | + } |
| 95 | + ] |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +Format a column before CSV escaping: |
| 100 | + |
| 101 | +```ts |
| 102 | +jsonToCsv(rows, { |
| 103 | + columns: [ |
| 104 | + { |
| 105 | + key: 'total', |
| 106 | + accessor: (row) => row.totalCents, |
| 107 | + formatter: (value) => `$${Number(value) / 100}` |
| 108 | + } |
| 109 | + ] |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +## CSV safety |
| 114 | + |
| 115 | +Values are escaped according to normal CSV rules: |
| 116 | + |
| 117 | +- fields containing the delimiter are quoted |
| 118 | +- fields containing newlines are quoted |
| 119 | +- quotes inside quoted fields are doubled |
| 120 | + |
| 121 | +```ts |
| 122 | +jsonToCsv([{ name: 'Ada, Lovelace', note: 'Line 1\n"Line 2"' }]); |
| 123 | +``` |
| 124 | + |
| 125 | +```csv |
| 126 | +name,note |
| 127 | +"Ada, Lovelace","Line 1 |
| 128 | +""Line 2""" |
| 129 | +``` |
| 130 | + |
| 131 | +When exporting to spreadsheets, use `escapeFormulae` to reduce formula-injection risk: |
| 132 | + |
| 133 | +```ts |
| 134 | +jsonToCsv([{ value: '=SUM(A1:A2)' }], { |
| 135 | + escapeFormulae: true |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +```csv |
| 140 | +value |
| 141 | +'=SUM(A1:A2) |
| 142 | +``` |
| 143 | + |
| 144 | +## Options |
| 145 | + |
| 146 | +```ts |
| 147 | +interface JsonToCsvOptions<TRecord> { |
| 148 | + columns?: Array<string | CsvColumn<TRecord>>; |
| 149 | + includeHeaders?: boolean; |
| 150 | + flatten?: boolean; |
| 151 | + sortColumns?: boolean; |
| 152 | + delimiter?: string; |
| 153 | + newline?: string; |
| 154 | + quote?: string; |
| 155 | + nullValue?: string; |
| 156 | + arrayMode?: 'json' | 'join' | 'empty'; |
| 157 | + arraySeparator?: string; |
| 158 | + escapeFormulae?: boolean | string; |
| 159 | + dateFormatter?: (date: Date) => string; |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +| Option | Default | Meaning | |
| 164 | +| --- | --- | --- | |
| 165 | +| `columns` | inferred | explicit column list | |
| 166 | +| `includeHeaders` | `true` | include the first header row | |
| 167 | +| `flatten` | `true` | flatten nested plain objects into dot paths | |
| 168 | +| `sortColumns` | `false` | sort inferred columns alphabetically | |
| 169 | +| `delimiter` | `','` | field delimiter | |
| 170 | +| `newline` | `'\n'` | line separator | |
| 171 | +| `quote` | `'"'` | quote character | |
| 172 | +| `nullValue` | `''` | output for `null` and `undefined` | |
| 173 | +| `arrayMode` | `'json'` | format arrays as JSON, joined text or empty | |
| 174 | +| `arraySeparator` | `', '` | separator used by `arrayMode: 'join'` | |
| 175 | +| `escapeFormulae` | `false` | prefix spreadsheet-like formulas | |
| 176 | +| `dateFormatter` | ISO string | format `Date` values | |
| 177 | + |
| 178 | +## Ecosystem recipes |
| 179 | + |
| 180 | +Use with `object-key-paths` to inspect columns before exporting: |
| 181 | + |
| 182 | +```ts |
| 183 | +import { getLeafPaths } from 'object-key-paths'; |
| 184 | +import { jsonToCsv } from 'json-csv-kit'; |
| 185 | + |
| 186 | +const columns = getLeafPaths(report).map((path) => ({ |
| 187 | + key: path, |
| 188 | + header: path |
| 189 | +})); |
| 190 | + |
| 191 | +const csv = jsonToCsv([report], { columns }); |
| 192 | +``` |
| 193 | + |
| 194 | +Use with `object-path-kit` when source paths need bracket notation: |
| 195 | + |
| 196 | +```ts |
| 197 | +import { getPath } from 'object-path-kit'; |
| 198 | +import { jsonToCsv } from 'json-csv-kit'; |
| 199 | + |
| 200 | +const csv = jsonToCsv(rows, { |
| 201 | + columns: [ |
| 202 | + { |
| 203 | + key: 'city', |
| 204 | + header: 'City', |
| 205 | + accessor: (row) => getPath(row, 'customer["billing.address"].city') |
| 206 | + } |
| 207 | + ] |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | +Use with `array-table-kit` when you need both Markdown and CSV exports from the same records: |
| 212 | + |
| 213 | +```ts |
| 214 | +import { arrayToMarkdownTable } from 'array-table-kit'; |
| 215 | +import { jsonToCsv } from 'json-csv-kit'; |
| 216 | + |
| 217 | +const markdown = arrayToMarkdownTable(rows); |
| 218 | +const csv = jsonToCsv(rows); |
| 219 | +``` |
| 220 | + |
| 221 | +## Notes |
| 222 | + |
| 223 | +- Input must be an array of plain objects. |
| 224 | +- Dot paths are intentionally simple. Use `accessor` or `object-path-kit` for bracket notation and keys containing dots. |
| 225 | +- Arrays are serialized as JSON by default so no information is lost. |
| 226 | +- `BigInt` values inside arrays or objects are converted to strings during JSON serialization. |
| 227 | +- Circular references are represented as `[Circular]` instead of crashing the export. |
| 228 | +- Class instances, `Map`, `Set` and other non-plain objects are serialized as JSON when possible. |
| 229 | + |
| 230 | +## License |
| 231 | + |
| 232 | +MIT |
0 commit comments