Methods
# csvStringToJson(csvString) → {Array.<object>}
Parse a CSV string and return as JSON array of objects
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.browser.csvStringToJson('name,age\nAlice,30');
console.log(rows); // [{ name: 'Alice', age: '30' }]
# csvStringToJsonAsync(csvString) → {Promise.<Array.<object>>}
Parse a CSV string asynchronously (returns resolved Promise)
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If csvString is invalid
If CSV is malformed
Promise resolving to array of objects
Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const rows = await csvToJson.browser.csvStringToJsonAsync('name,age\nAlice,30');
console.log(rows);
# csvStringToJsonStringified(csvString) → {string}
Parse a CSV string and return as stringified JSON
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If csvString is invalid
If CSV is malformed
JSON stringified array of objects
string
Example
const csvToJson = require('convert-csv-to-json');
const jsonString = csvToJson.browser.csvStringToJsonStringified('name,age\nAlice,30');
console.log(jsonString);
# csvStringToJsonStringifiedAsync(csvString) → {Promise.<string>}
Parse a CSV string asynchronously and return as stringified JSON
Parameters:
| Name | Type | Description |
|---|---|---|
csvString |
string
|
CSV content as string |
If csvString is invalid
If CSV is malformed
Promise resolving to JSON stringified array
Promise.<string>
Example
const csvToJson = require('convert-csv-to-json');
const json = await csvToJson.browser.csvStringToJsonStringifiedAsync('name,age\nAlice,30');
console.log(json);
# 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 | Default | Description |
|---|---|---|---|
active |
boolean
|
true | Whether to format values by type (default: true) |
For method chaining
this
# indexHeader(index) → {this}
Set the row index where CSV headers are located
Parameters:
| Name | Type | Description |
|---|---|---|
index |
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
# parseFile(file, optionsopt) → {Promise.<Array.<object>>}
Parse a browser File or Blob object to JSON array.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
file |
File
|
Blob
|
File or Blob to read as text |
|
options |
object
|
<optional> |
options: { encoding?: string } |
Promise resolving to parsed JSON rows
Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const fileInput = document.querySelector('#csvfile').files[0];
const rows = await csvToJson.browser.parseFile(fileInput);
console.log(rows);
# 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 | Default | Description |
|---|---|---|---|
active |
boolean
|
false | Whether to support quoted fields (default: false) |
For method chaining
this
# trimHeaderFieldWhiteSpace(active) → {this}
Configure whitespace handling in header field names
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
active |
boolean
|
false | If true, removes all whitespace; if false, only trims edges (default: false) |
For method chaining
this