Members
# constant csvToJsonAsync
Parse CSV file asynchronously and return parsed data as JSON array
Example
const csvToJson = require('convert-csv-to-json');
const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
console.log(data);
Methods
# asciiEncoding() → {object}
Set ASCII encoding for reading files
Module context for method chaining
object
# base64Encoding() → {object}
Set Base64 encoding for reading files
Module context for method chaining
object
# csvStringToJson(csvString) → {Array.<object>}
Parse a CSV string and return as JSON array of objects (synchronous)
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If csvString is invalid
If CSV is malformed
Array of objects representing CSV rows
Array.<object>
Example
const csvToJson = require('convert-csv-to-json');
const rows = csvToJson.csvStringToJson('name,age\nAlice,30');
console.log(rows); // [{ name: 'Alice', age: '30' }]
# csvStringToJsonStringified(csvString) → {string}
Parse CSV string and return as stringified JSON (synchronous)
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If csvString is invalid
If CSV is malformed
If JSON generation fails
JSON stringified array of objects
string
# customEncoding(encoding) → {object}
Set custom file encoding for reading CSV files Useful for non-UTF8 encoded files
Parameters:
| Name | Type | Description |
|---|---|---|
encoding |
string
|
Node.js supported encoding (e.g., 'utf8', 'latin1', 'ascii') |
Module context for method chaining
object
# fieldDelimiter(delimiter) → {object}
Set the field delimiter character used to separate CSV fields
Parameters:
| Name | Type | Description |
|---|---|---|
delimiter |
string
|
Character(s) to use as field separator (default: ',') |
Module context for method chaining
object
# formatValueByType(active) → {object}
Enable or disable automatic type formatting for values Converts numeric strings to numbers, 'true'/'false' to booleans
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to format values by type (default: true) |
Module context for method chaining
object
# generateJsonFileFromCsv(inputFileName, outputFileName)
Parse CSV file and write the parsed JSON to an output file (synchronous)
Parameters:
| Name | Type | Description |
|---|---|---|
inputFileName |
string
|
Path to input CSV file |
outputFileName |
string
|
Path to output JSON file |
If inputFileName or outputFileName is not defined
Error
If file operations fail
If CSV is malformed
Example
const csvToJson = require('convert-csv-to-json');
csvToJson.generateJsonFileFromCsv('input.csv', 'output.json');
# getJsonFromCsv(inputFileName) → {Array.<object>}
Parse CSV file and return parsed data as JSON array of objects (synchronous)
Parameters:
| Name | Type | Description |
|---|---|---|
inputFileName |
string
|
Path to input CSV file |
If inputFileName is not defined
Error
If file read fails
If CSV is malformed
Array of objects representing CSV rows
Array.<object>
Example
const csvToJson = require('convert-csv-to-json');
const rows = csvToJson.getJsonFromCsv('resource/input.csv');
console.log(rows);
# hexEncoding() → {object}
Set Hex encoding for reading files
Module context for method chaining
object
# indexHeader(index) → {object}
Set the row index where CSV headers are located Use this if headers are not on the first line (row 0)
Parameters:
| Name | Type | Description |
|---|---|---|
index |
number
|
Zero-based row index containing headers |
Module context for method chaining
object
# latin1Encoding() → {object}
Set Latin-1 (ISO-8859-1) encoding for reading files
Module context for method chaining
object
# mapRows(mapperFn) → {object}
Set a mapper function to transform each row after conversion The mapper function receives (row, index) where row is the JSON object and index is the 0-based row number. Return null/undefined to filter out rows.
Parameters:
| Name | Type | Description |
|---|---|---|
mapperFn |
function
|
Function to transform each row |
Module context for method chaining
object
Example
csvToJson
.mapRows((row, idx) => idx % 2 === 0 ? row : null) // Keep every other row
.getJsonFromCsv('input.csv')
# parseSubArray(delimiter, separator) → {object}
Configure sub-array parsing for special field values Fields bracketed by delimiter and containing separator are parsed into arrays
Parameters:
| Name | Type | Description |
|---|---|---|
delimiter |
string
|
Bracket character (default: '*') |
separator |
string
|
Item separator within brackets (default: ',') |
Module context for method chaining
object
Example
// Input field: "*val1,val2,val3*"
// Output array: ["val1", "val2", "val3"]
csvToJson.parseSubArray('*', ',')
# supportQuotedField(active) → {object}
Enable or disable support for RFC 4180 quoted fields When enabled, fields wrapped in double quotes can contain delimiters and newlines
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to support quoted fields (default: false) |
Module context for method chaining
object
# trimHeaderFieldWhiteSpace(active) → {object}
Configure whitespace handling in CSV header field names When active, removes all whitespace from header names (e.g., "My Name" → "MyName") When inactive, only trims leading and trailing whitespace
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to remove all whitespace from headers (default: false) |
Module context for method chaining
object
# ucs2Encoding() → {object}
Set UCS-2 encoding for reading files
Module context for method chaining
object
# utf16leEncoding() → {object}
Set UTF-16 LE encoding for reading files
Module context for method chaining
object
# utf8Encoding() → {object}
Set UTF-8 encoding (default encoding)
Module context for method chaining
object