Skip to content
This repository was archived by the owner on Nov 2, 2021. It is now read-only.

GRFreire/csv2json-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

csv2json-parser

Convert csv to json and vice versa

Installation

  • Npm
npm install csv2json-parser --save
  • Yarn
yarn add csv2json-parser

Usage

Converting CSV to JSON

simple method:

const { toJSON } = require('csv2json-parser');

const objectArray = toJSON(csv_string); //returns an objectArray(default) object format

using diferents object formats:

const objectArray = toJSON(csv_string, { type: 'keyArray' }); //returns an keyArray object format

using keyed object format:

  • keep primaryKey empty for using the first key as primary key
const objectArray = toJSON(csv_string, {
    type: 'keyed',
    primaryKey: 'name'
}); //returns an keyed object format

limiting length:

const objectArray = toJSON(csv_string, { maxLength: 5 }); //returns an objectArray(default) object format with max length of 5

Converting CSV to JSON

const { toCSV } = require('csv2json-parser');

const csv = toCSV(object_or_array); //returns a csv string

Object formats

  • objectArray

[
   {
       "id": 0,
       "country": "Brazil",
       "region": "South America"
   },
   {
       "id": 1,
       "country": "United States",
       "region": "North America"
   }
]
  • keyArray

{
  "id":[0,1],
  "country":["Brazil", "United States"],
  "region":["South America", "North America"]
}
  • keyed

{
  "0": {
     "country": "Brazil",
     "region": "South America"
  },
  "1": {
     "country": "United States",
     "region": "North America"
  }
}

Object formats supported for parsing to CSV

  • objectArray
  • keyArray

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published