1.1.6 • Published 5 months ago

wk-csv-json v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

idfirst namelast nameemail
1LetiziaDominyLetizia.Dominy@yopmail.com
2WilmaTherineWilma.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)

ParametersTypeDefaultDescription
targetFileListThe CSV file to be converted to JSON
headersBooleantrueUse the header as key

csvToJSON(target, headers)

ParametersTypeDefaultDescription
targetStringThe CSV string to be converted to JSON
headersBooleantrueUse 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

1.1.6

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago