Methods
# csvStringToJson(csvString) → {Array.<object>}
Parse CSV string content and return as JSON array of objects
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
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 content and return as stringified JSON
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If CSV is malformed
If JSON generation fails
JSON stringified array of objects
string
Example
const csvToJson = require('convert-csv-to-json');
const jsonString = csvToJson.csvStringToJsonStringified('name,age\nAlice,30');
console.log(jsonString);
# encoding(encoding) → {this}
Set file encoding for reading CSV files
Parameters:
| Name | Type | Description |
|---|---|---|
encoding |
string
|
Node.js supported encoding (e.g., 'utf8', 'latin1', 'ascii') |
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 (default: ',') |
For method chaining
this
# formatValueByType(active) → {this}
Enable or disable automatic type formatting for values When enabled, numeric strings are converted to numbers, 'true'/'false' to booleans
Parameters:
| Name | Type | Description |
|---|---|---|
active |
boolean
|
Whether to format values by type |
For method chaining
this
# generateJsonFileFromCsv(fileInputName, fileOutputName)
Read a CSV file and write the parsed JSON to an output file
Parameters:
| Name | Type | Description |
|---|---|---|
fileInputName |
string
|
Path to input CSV file |
fileOutputName |
string
|
Path to output JSON file |
If file read or write fails
If CSV is malformed
# getJsonFromCsv(fileInputName) → {Array.<object>}
Read a CSV file and return parsed data as JSON array of objects
Parameters:
| Name | Type | Description |
|---|---|---|
fileInputName |
string
|
Path to input CSV file |
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);
# getJsonFromCsvStringified(fileInputName) → {string}
Read a CSV file and return parsed data as stringified JSON
Parameters:
| Name | Type | Description |
|---|---|---|
fileInputName |
string
|
Path to input CSV file |
If file read fails
If CSV is malformed
If JSON generation fails
JSON stringified array of objects
string
Example
const csvToJson = require('convert-csv-to-json');
const jsonString = csvToJson.getJsonFromCsvStringified('resource/input.csv');
console.log(jsonString);
# getLineEndingLength(content, index) → {number}
Get the length of line ending at current position (CRLF=2, LF=1, CR=1, or 0)
Parameters:
| Name | Type | Description |
|---|---|---|
content |
string
|
CSV content |
index |
number
|
Current index to check |
Length of line ending (0 if none)
number
# indexHeader(indexHeaderValue) → {this}
Set the row index where CSV headers are located
Parameters:
| Name | Type | Description |
|---|---|---|
indexHeaderValue |
number
|
Zero-based row index containing headers |
If not a valid number
For method chaining
this
# mapRows(mapperFn) → {this}
Sets a mapper function to transform each row after conversion
Parameters:
| Name | Type | Description |
|---|---|---|
mapperFn |
function
|
Function that receives (row, index) and returns transformed row or null to filter out |
For method chaining
this
# parseRecords(csvContent) → {Array.<string>}
Parse CSV content into individual records, respecting quoted fields that may span multiple lines. RFC 4180 compliant parsing - handles quoted fields that may contain newlines.
Parameters:
| Name | Type | Description |
|---|---|---|
csvContent |
string
|
The raw CSV content |
Array of record strings
Array.<string>
# parseSubArray(delimiter, separator) → {this}
Configure sub-array parsing for special field values Fields bracketed by delimiter and containing separator are parsed into arrays
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
delimiter |
string
|
* | Bracket character (default: '*') |
separator |
string
|
, | Item separator within brackets (default: ',') |
For method chaining
this
Example
// Input: "*val1,val2,val3*"
// Output: ["val1", "val2", "val3"]
.parseSubArray('*', ',')
# split(line) → {Array.<string>}
Split a CSV record line into fields, respecting quoted fields per RFC 4180. Handles:
- Quoted fields with embedded delimiters and newlines
- Escaped quotes (double quotes within quoted fields)
- Empty quoted fields
Parameters:
| Name | Type | Description |
|---|---|---|
line |
string
|
A single CSV record line |
Array of field values
Array.<string>
# supportQuotedField(active) → {this}
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 |
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 from header names; if false, only trims edges |
For method chaining
this