@srihari_007/csv-to-json-converter

1.1.0 • Public • Published

CSV-TO-JSON

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.

Installation

npm install @srihari_007/csv-to-json-converter

Usage

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"],
]);

Error Handling

The csvtojson object has four methods: fromFilePath, fromString, fromArray, and 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.

Notes

  • The fromFilePath method expects the file path to be specified as a string.
  • The fromString, fromArray, and fromTwoDimensonArray methods 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/False will be converted to boolean, strings will be left as strings).

Motivation

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.

Author

fw_qaq fw_qaq

License

csv-to-json-converter is released under the MIT license.

Package Sidebar

Install

npm i @srihari_007/csv-to-json-converter

Weekly Downloads

8

Version

1.1.0

License

MIT

Unpacked Size

35.4 kB

Total Files

17

Last publish

Collaborators

  • srihari_007