CSV to JSON converter is a lightweight and powerful utility that uses node.js to convert CSV files to JSON in an easy and customizable manner.
npm install @srihari_007/csv-to-json-converter
const csvtojson = require("@srihari_007/csv-to-json-converter"); // Convert a CSV file at the specified file path to a JSON object const jsonFromFilePath = csvtojson().fromFilePath("/path/to/file.csv"); // Convert a CSV string to a JSON object const jsonFromString = csvtojson().fromString( "header1,header2\nvalue1,value2\nvalue3,value4" ); // Convert a CSV array to a JSON object const jsonFromArray = csvtojson().fromArray([ "header1,header2", "value1,value2", "value3,value4", ]); // Convert a CSV two-dimensional array to a JSON object const jsonFromTwoDArray = csvtojson().fromTwoDimensonArray([ ["header1", "header2"], ["value1", "value2"], ["value3", "value4"], ]);
csvtojson object has four methods:
fromTwoDimensonArray. If an error occurs while using one of these methods, an error will be thrown with a message indicating the cause of the error.
The possible error messages are:
Invalid file: The file specified by filePath is empty or contains only the headers.
Invalid path: The file specified by filePath does not exist.
Invalid file format: The file specified by filePath is not a CSV file.
Invalid syntax: The number of headers does not match the number of values in a row of the CSV file.
fromFilePathmethod expects the file path to be specified as a string.
fromTwoDimensonArraymethods expect the CSV data to be in the form of a string, array, or two-dimensional array, respectively.
- The first row of the CSV data is assumed to contain the headers.
- The values in the JSON objects will be of the appropriate data type (e.g. numbers will be converted to numbers,
True/Falsewill be converted to boolean, strings will be left as strings).
The reason for this tool is that I was in charge of converting a couple of csv files to JSON and I couldn't find anything simple that did exactly what I expected. So I built this to be flexible enough for any file.
csv-to-json-converter is released under the MIT license.