You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package is compatible with  and .
**NPM** (see [Installing Npm](https://docs.npmjs.com/getting-started/installing-node)).
107
135
108
136
## Install npm *convert-csv-to-json package*
109
137
Go to NPM package [convert-csv-to-json](https://www.npmjs.com/package/convert-csv-to-json).
110
138
111
-
### Install
112
139
Install package in your *package.json*
113
140
```bash
114
141
$ npm install convert-csv-to-json --save
@@ -357,10 +384,138 @@ let jsonArray = csvToJson
357
384
.csvStringToJson(csvString);
358
385
```
359
386
387
+
### Sync API (TypeScript)
388
+
389
+
TypeScript typings are available via the included `index.d.ts`. You can importthedefaultconverterorusenamedimports. BelowarecommonpatternswhenusingthesynchronousAPIfrom TypeScript.
390
+
391
+
```ts
392
+
// Named import (recommended when using ES modules)
393
+
import converter, { /* or */ } from 'convert-csv-to-json';
394
+
// Access the default converter
395
+
const csvToJson = require('convert-csv-to-json');
396
+
397
+
// Define a type for your CSV records
398
+
interface Person {
399
+
name: string;
400
+
age: number;
401
+
}
402
+
403
+
// Parse CSV string synchronously and assert the returned type
404
+
const csv = 'name,age\nAlice,30';
405
+
const parsed = csvToJson.csvStringToJson(csv) as Person[];
406
+
407
+
// Chain configuration and call sync methods
408
+
const result = csvToJson
409
+
.fieldDelimiter(',')
410
+
.formatValueByType()
411
+
.csvStringToJson('name,age\nBob,25') as Person[];
412
+
```
413
+
414
+
## Browser API Usage
415
+
416
+
The package exposes a `browser` helper that reuses the library's parsing logic but provides browser-friendly helpers for parsing CSV strings and `File`/`Blob` objects. The API mirrors the synchronous and asynchronous Node APIs and supports method chaining for configuration.
`parseFile` accepts an optional `options` object with `encoding` (passed to `FileReader.readAsText`). If `FileReader` is not available, `parseFile` will reject.
452
+
453
+
### Browser API Notes
454
+
455
+
- The `browser` API proxies the same configuration methods as the Node API and follows the same behavior for quoted fields, sub-array parsing, trimming, and value formatting.
456
+
- `parseFile` depends on the browser `FileReader` API; calling it in Node.js will reject with an informative error.
457
+
458
+
### Browser API (TypeScript)
459
+
460
+
TypeScript typings are provided via the included `index.d.ts`. You can import the default converter and access the `browser` helper, or import `browser` directly. Below are common usage patterns.
461
+
462
+
```ts
463
+
// Named import (recommended for direct use)
464
+
import { browser } from 'convert-csv-to-json';
465
+
466
+
// Or default import and access the browser helper
467
+
import converter from 'convert-csv-to-json';
468
+
const browserApi = converter.browser;
469
+
470
+
// Define a type for your CSV records
471
+
interface Person {
472
+
name: string;
473
+
age: number;
474
+
}
475
+
476
+
// Synchronous parse (assert the returned type)
477
+
const csv = 'name,age\nAlice,30';
478
+
const parsed = browser.csvStringToJson(csv) as Person[];
479
+
480
+
// Async parse
481
+
const parsedAsync = await browser.csvStringToJsonAsync(csv) as Person[];
482
+
483
+
// Parse a File in the browser
484
+
const inputEl = document.querySelector('input[type=file]') as HTMLInputElement;
485
+
const file = inputEl.files![0];
486
+
const data = await browser.parseFile(file) as Person[];
487
+
```
488
+
489
+
The `BrowserApi` interface in `index.d.ts` exposes typed method signatures for IDE autocompletion and compile-time checks.
490
+
360
491
## Async API Usage
361
492
362
493
This library provides a Promise-based async API that's perfect for modern Node.jsapplications. For a detailed migration guide from sync to asyncAPI, see [MIGRATION.md](MIGRATION.md).
363
494
495
+
### Async API (TypeScript)
496
+
497
+
The asyncAPI also has TypeScript typings. Typical usage in TypeScript looks like this:
0 commit comments