Class

BrowserApi

BrowserApi()

Browser-friendly CSV to JSON API Provides methods for parsing CSV strings and File/Blob objects in browser environments Proxies configuration to sync csvToJson instance

Constructor

# new BrowserApi()

Constructor initializes proxy to sync csvToJson instance

View Source src/browserApi.js, line 15

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

View Source src/browserApi.js, line 106

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

View Source src/browserApi.js, line 152

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

View Source src/browserApi.js, line 129

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

View Source src/browserApi.js, line 167

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

View Source src/browserApi.js, line 49

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)

View Source src/browserApi.js, line 29

For method chaining

this

# async getJsonFromFileStreamingAsync(file) → {Promise.<Array.<object>>}

Parse CSV from a File object using streaming for memory-efficient processing

Parameters:
Name Type Description
file File

File object containing CSV data

View Source src/browserApi.js, line 265

If file is invalid

If streaming is not supported or parsing fails

Promise resolving to array of objects representing CSV rows

Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const fileInput = document.querySelector('#csvfile').files[0];
const data = await csvToJson.browser.getJsonFromFileStreamingAsync(fileInput);
console.log(data);

# async getJsonFromFileStreamingAsyncWithCallback(file, options) → {Promise.<void>}

Parse CSV from a File object using streaming with progress callbacks for large files Processes data in chunks to avoid memory issues with large datasets

Parameters:
Name Type Attributes Default Description
file File

File object containing CSV data

options object

Processing options

onChunk function

Callback for each chunk of processed rows

onComplete function <optional>

Callback when processing is complete

onError function <optional>

Callback for errors

chunkSize number <optional>
1000

Number of rows per chunk

View Source src/browserApi.js, line 315

If file or options are invalid

Promise that resolves when streaming starts

Promise.<void>
Example
const csvToJson = require('convert-csv-to-json');
const fileInput = document.querySelector('#csvfile').files[0];

await csvToJson.browser.getJsonFromFileStreamingAsyncWithCallback(fileInput, {
  chunkSize: 500,
  onChunk: (rows, processed, total) => {
    console.log(`Processed ${processed}/${total} rows`);
    // Handle chunk of rows here
  },
  onComplete: (allRows) => {
    console.log('Processing complete!');
  },
  onError: (error) => {
    console.error('Error:', error);
  }
});

# async getJsonFromStreamAsync(stream) → {Promise.<Array.<object>>}

Parse CSV from a browser ReadableStream and return parsed data as JSON array Processes data in chunks for memory-efficient handling of large streams

Parameters:
Name Type Description
stream object

Browser ReadableStream containing CSV data

View Source src/browserApi.js, line 235

If stream is invalid

If streaming is not supported or parsing fails

Promise resolving to array of objects representing CSV rows

Promise.<Array.<object>>
Example
const csvToJson = require('convert-csv-to-json');
const response = await fetch('large-dataset.csv');
const stream = response.body;
const data = await csvToJson.browser.getJsonFromStreamAsync(stream);
console.log(data);

# indexHeader(index) → {this}

Set the row index where CSV headers are located

Parameters:
Name Type Description
index number

Zero-based row index containing headers

View Source src/browserApi.js, line 69

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

View Source src/browserApi.js, line 90

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 }

View Source src/browserApi.js, line 182

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: ',')

View Source src/browserApi.js, line 80

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)

View Source src/browserApi.js, line 39

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)

View Source src/browserApi.js, line 59

For method chaining

this