wk-csv-json
v1.1.6
Published
Convert an uploaded CSV file to JSON and CSV string
Downloads
3
Maintainers
Readme
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 | [email protected] | | 2 | Wilma | Therine | [email protected] |
Sample Output (Headers true)
{
data: [
{
"id": 1,
"firstname": "Letizia",
"lastname": "Dominy",
"email": "[email protected]"
},
{
"id": 2,
"firstname": "Wilma",
"lastname": "Therine",
"email": "[email protected]"
}
],
message: "Conversion complete"
}
Sample Output (Headers false)
{
data: [
{
"value": ["id", "first name", "last name", "email"]
},
{
"value": [1, "Letizia", "Dominy", "[email protected]"]
},
{
"value": [2, "Wilma", "Therine", "[email protected]"]
}
],
message: "Conversion complete"
}
csvToJSON method
// on your component.ts
this.csv = csvToJSON(
"id,name,email\n1,Letizia Dominy,[email protected]"
);
Output (Headers true)
{
data: [
{
"id": 1,
"name": "Letizia Dominy",
"email": "[email protected]"
}
],
message: "Conversion complete"
}
Output (Headers false)
{
data: [
{
"value": ["id", "name", "email"]
},
{
"value": [1, "Letizia Dominy", "[email protected]"]
}
],
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