Methods
# csvStringToJsonAsync(csvString, options) → {Promise.<Array.<object>>}
Parse CSV string to JSON array (async)
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
options |
object
|
Configuration options (default: { raw: true }) |
If CSV is malformed
Array of objects representing CSV rows
Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const data = await csvToJson.csvStringToJsonAsync('name,age\nAlice,30');
console.log(data);
# encoding(encoding) → {this}
Set file encoding for reading CSV files
Parameters:
| Name | Type | Description |
|---|---|---|
encoding |
string
|
Node.js supported encoding (e.g., 'utf8', 'latin1') |
For method chaining
this
# fieldDelimiter(delimiter) → {this}
Set the field delimiter character
Parameters:
| Name | Type | Description |
|---|---|---|
delimiter |
string
|
Character(s) to use as field separator |
For method chaining
this
# formatValueByType(active) → {this}
Enable or disable automatic type formatting for values
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to format values by type |
For method chaining
this
# async generateJsonFileFromCsv(fileInputName, fileOutputName) → {Promise.<void>}
Read a CSV file and write parsed JSON to an output file (async)
Parameters:
| Name | Type | Description |
|---|---|---|
fileInputName |
string
|
Path to input CSV file |
fileOutputName |
string
|
Path to output JSON file |
If file operations fail
If CSV is malformed
Promise.<void>
# async getJsonFromCsvAsync(inputFileNameOrCsv, options) → {Promise.<Array.<object>>}
Main async API method for reading CSV and returning parsed JSON Supports reading from file path or parsing CSV string content
Parameters:
| Name | Type | Description |
|---|---|---|
inputFileNameOrCsv |
string
|
File path or CSV string content |
options |
object
|
Configuration options |
raw |
boolean
|
If true, treats input as CSV string; if false, reads from file |
If input is invalid
If file read fails
If CSV is malformed
Array of objects representing CSV rows
Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
console.log(data);
# async getJsonFromCsvStringified(fileInputName) → {Promise.<string>}
Read a CSV file and return parsed data as stringified JSON (async)
Parameters:
| Name | Type | Description |
|---|---|---|
fileInputName |
string
|
Path to input CSV file |
If file read fails
If CSV is malformed
JSON stringified array of objects
Promise.<string>
# async getJsonFromFileStreamingAsync(filePath) → {Promise.<Array.<object>>}
Parse CSV from a file path using streaming for memory-efficient processing
Parameters:
| Name | Type | Description |
|---|---|---|
filePath |
string
|
Path to the CSV file |
If filePath is invalid
If file cannot be read
If CSV is malformed
Promise resolving to array of objects representing CSV rows
Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const data = await csvToJson.getJsonFromFileStreamingAsync('large.csv');
console.log(data);
# async getJsonFromStreamAsync(stream) → {Promise.<Array.<object>>}
Parse CSV from a Readable stream and return parsed data as JSON array Processes data in chunks for memory-efficient handling of large files
Parameters:
| Name | Type | Description |
|---|---|---|
stream |
object
|
Node.js Readable stream containing CSV data |
If stream is invalid
If CSV is malformed
Promise resolving to array of objects representing CSV rows
Promise.<Array.<object>>
Example
const fs = require('fs');
const csvToJson = require('convert-csv-to-json');
const stream = fs.createReadStream('large.csv');
const data = await csvToJson.getJsonFromStreamAsync(stream);
console.log(data);
# indexHeader(indexHeader) → {this}
Set the row index where CSV headers are located
Parameters:
| Name | Type | Description |
|---|---|---|
indexHeader |
number
|
Zero-based row index containing headers |
For method chaining
this
# mapRows(mapperFn) → {this}
Set a mapper function to transform each row after conversion
Parameters:
| Name | Type | Description |
|---|---|---|
mapperFn |
function
|
Function receiving (row, index) that returns transformed row or null to filter |
For method chaining
this
# parseSubArray(delimiter, separator) → {this}
Configure sub-array parsing for special field values
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
delimiter |
string
|
* | Bracket character (default: '*') |
separator |
string
|
, | Item separator within brackets (default: ',') |
For method chaining
this
# supportQuotedField(active) → {this}
Enable or disable support for RFC 4180 quoted fields
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to support quoted fields |
For method chaining
this
# trimHeaderFieldWhiteSpace(active) → {this}
Configure whitespace handling in header field names
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
If true, removes all whitespace; if false, only trims edges |
For method chaining
this