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);
# csvToJsonWithConfig(parsedCsv, config) → {Array.<object>}
Parse CSV content using a frozen configuration snapshot.
Parameters:
| Name | Type | Description |
|---|---|---|
parsedCsv |
string
|
Raw CSV content as string |
config |
ParserConfig
|
Frozen parser configuration |
Parsed JSON array
Array.<object>
# 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
# 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>
# split(line, configopt) → {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 | Attributes | Description |
|---|---|---|---|
line |
string
|
A single CSV record line |
|
config |
ParserConfig
|
<optional> |
Parser configuration |
Array of field values
Array.<string>