# wk-csv-json

> Convert an uploaded CSV file to JSON and CSV string

Latest version **1.1.6** (published 2023-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install wk-csv-json
pnpm add wk-csv-json
yarn add wk-csv-json
bun add wk-csv-json
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2023-11-28 |
| First published | 2023-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Joaquin Guese |
| Maintainers | wakwakin |
| Keywords | typescript, input file, upload, csv, csv to json, json, convert |

## Links

- npm: https://www.npmjs.com/package/wk-csv-json
- Repository: https://github.com/wakwakin/wk-csv-json
- Homepage: https://github.com/wakwakin/wk-csv-json#readme
- Issues: https://github.com/wakwakin/wk-csv-json/issues
- npm.io page: https://npm.io/package/wk-csv-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.6 (latest) — 2023-11-28
- 1.1.5 — 2023-11-28
- 1.1.4 — 2023-11-28
- 1.1.3 — 2023-11-28
- 1.0.3 — 2023-11-27
- 1.0.2 — 2023-11-26
- 1.0.1 — 2023-11-26
- 1.0.0 — 2023-11-26

## README

## Installation

```bash
  npm install wk-csv-json
```

## Usage

inputToJSON method
```typescript
// 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
  })
}
```

```html
<!-- 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)

```typescript
{
  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)

```typescript
{
  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
```typescript
// on your component.ts

this.csv = csvToJSON(
  "id,name,email\n1,Letizia Dominy,Letizia.Dominy@yopmail.com"
);
```

Output (Headers true)

```typescript
{
  data: [
    {
      "id": 1,
      "name": "Letizia Dominy",
      "email": "Letizia.Dominy@yopmail.com"
    }
  ],
  message: "Conversion complete"
}
```

Output (Headers false)

```typescript
{
  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

---
_Source: https://npm.io/package/wk-csv-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
