@novigi/csv-to-json

1.0.0-2Β β€’Β PublicΒ β€’Β Published

npm (scoped) NPM Statements Branches Functions Lines

@novigi/csv-to-json

Simple library for csv parse to Javascript object from any CSV string or csv file πŸš€

🐿 Features

  • Chainable and immutable API
  • Parse CSV content into JSON object
  • Parse CSV files into JSON objects
  • Supports custom headers

πŸ“¦ Getting Started

  1. Install the dependency
npm install @novigi/csv-to-json
  1. Import the library
const lib = require('@novigi/csv-to-json');

πŸ“– Documentation

csv-to-json

This library contains methods that allow user to parse the csv, either it is a csv string or file convert into the Javscript object.

This guideline is about the formats and examples that can follow for csv string parsing to Javascript object conversion πŸš€

const csv = require('@novigi/csv-to-json')

const input = 'name,age,sex\nkasun,33,male\nsteve,35,male'
let json = csv().parseData(input)

// or

let json = csv().parseFile(path/to/file)

Chainable + immutable methods! ☝

csv-to-json~Csv

Kind: inner class of csv-to-json

csv.delimiter β‡’ Csv

Delimiter is for different separators that can manually pass with csv file or string, the separator separates the CSV string in the context.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
delimiter string The delimiter as a parameter to separate the values in the csv

Example

const csvString = "name;age;sex\nkasun;33;male\nsteve;35;male"
csv().fieldDelimiter(';').parseData(csvString)   // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.lineBreak β‡’ Csv

Line-break is for for indicating the ending of a row in the csv content.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
lineBreak string The lineBreak as a parameter to separate the lines in the csv

Example

const csvString = "name,age,sex\r\nkasun,33,male\r\nsteve,35,male"
csv().lineBreak('\r\n').parseData(csvString)   // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.noHeaders β‡’ Csv

This method is to indicate whether csv content has the header data or not.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
noHeaders * Indicate whether csv content has headers. Undefined parameter is considered as true

Example

const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().noHeaders().headers(customHeader).parseData(csvString) // custom headers wil be used as the headers of the csv content

csv.headers β‡’ Csv

Adds headers to use as the headers of the csv content. Specifying headers will override the headers in the csv content. When provided header list is insufficient, headers in the csv content are considered

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
headers array Custom header array to be used as headers of the csv content

Example

const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().headers(customHeader).parseData(csvString)

csv.parseFile β‡’ Csv

CSV file read and parse to javascript data structure , 'parseFile' method read and convert the csv file into the javascript array object.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
csvFile string valid csv file path , the path passes as a parameter and read the file in that file is exist

Example

const path = './resources/csvFile.csv'           // csv file directory path
csv().parseFile(csvFile)                           // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.parseData β‡’ Csv

CSV parse to javascript data structure,'parse' method convert the csv string to the javascript array object.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

Param Type Description
csvString string valid string to passes as a parameter

Example

const csvString = "name,age,sex\nkasun,33,male\nsteve,35,male"
csv().parseData(csvString)                              // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv-to-json~csv([options]) β‡’ Csv

Create an Csv chain builder instance

Kind: inner method of csv-to-json
Returns: Csv - new instance of 'Csv' object

Param Type Description
[options] object optional configurations for the chain
[options.delimiter] string delimiter which csv fields are separated
[options.lineBreak] string line break character of csv rows
[options.noHeaders] boolean falg to indicate csv content has a header row or not
[options.headers] object custom headers for parsing csv content

Example

csv()
csv(options)

This is an auto generated file. Please don't make changes manually

Readme

Keywords

none

Package Sidebar

Install

npm i @novigi/csv-to-json

Weekly Downloads

1

Version

1.0.0-2

License

MIT

Unpacked Size

17.5 kB

Total Files

4

Last publish

Collaborators

  • buddhima
  • nov_user
  • madushak