wk-csv-json
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

Installation

  npm install wk-csv-json

Usage

inputToJSON method

// on your component.ts

import { inputToJSON } from 'wk-csv-json'

public csv

fileChange(event) {
  const file = event.target.files

  inputToJSON(file).then((data) => {
    this.csv = data
  })
}
<!-- on your component.html -->

<input type="file" (change)="fileChange($event)" />

Sample CSV file

id first name last name email
1 Letizia Dominy Letizia.Dominy@yopmail.com
2 Wilma Therine Wilma.Therine@yopmail.com

Sample Output (Headers true)

{
  data: [
    {
      "id": 1,
      "firstname": "Letizia",
      "lastname": "Dominy",
      "email": "Letizia.Dominy@yopmail.com"
    },
    {
      "id": 2,
      "firstname": "Wilma",
      "lastname": "Therine",
      "email": "Wilma.Therine@yopmail.com"
    }
  ],
  message: "Conversion complete"
}

Sample Output (Headers false)

{
  data: [
    {
      "value": ["id", "first name", "last name", "email"]
    },
    {
      "value": [1, "Letizia", "Dominy", "Letizia.Dominy@yopmail.com"]
    },
    {
      "value": [2, "Wilma", "Therine", "Wilma.Therine@yopmail.com"]
    }
  ],
  message: "Conversion complete"
}

csvToJSON method

// on your component.ts

this.csv = csvToJSON(
  "id,name,email\n1,Letizia Dominy,Letizia.Dominy@yopmail.com"
);

Output (Headers true)

{
  data: [
    {
      "id": 1,
      "name": "Letizia Dominy",
      "email": "Letizia.Dominy@yopmail.com"
    }
  ],
  message: "Conversion complete"
}

Output (Headers false)

{
  data: [
    {
      "value": ["id", "name", "email"]
    },
    {
      "value": [1, "Letizia Dominy", "Letizia.Dominy@yopmail.com"]
    }
  ],
  message: "Conversion complete"
}

inputToJSON(target, headers)

Parameters Type Default Description
target FileList The CSV file to be converted to JSON
headers Boolean true Use the header as key

csvToJSON(target, headers)

Parameters Type Default Description
target String The CSV string to be converted to JSON
headers Boolean true Use the header as key

if headers parameter is true, it uses the first line in CSV as headers.

Future

Convert plain CSV (Not from file) to JSON - Added

Convert JSON to CSV

Package Sidebar

Install

npm i wk-csv-json

Weekly Downloads

2

Version

1.1.6

License

MIT

Unpacked Size

19.5 kB

Total Files

9

Last publish

Collaborators

  • wakwakin