How to use papa-parse-angular2 How to use papa-parse-angular2 json json

How to use papa-parse-angular2


You could use ngx-papaparse.

First install the library:

For Angular 6 (docs):

npm install ngx-papaparse --save

There is also a version for Angular 2/4 and Angular 5.

Then import it into your module (doesn't have to be your AppModule):

import { PapaParseModule } from 'ngx-papaparse';@NgModule({  ...  imports: [    ...    PapaParseModule  ]})

Parsing CSV:

import { Component } from '@angular/core';import { Papa } from 'ngx-papaparse';@Component({  ...})export class AppComponent {    constructor(private papa: Papa) {        let csvData = '"Hello","World!"';        this.papa.parse(csvData,{            complete: (result) => {                console.log('Parsed: ', result);            }        });    }}

You can also parse a file, instead of a string. Just replace csvData with the file.

Disclaimer: I created the library.


You can simply use papaparser.

import * as Papa from 'papaparse/papaparse.min.js';

To use

onUpload(file: File) {   Papa.parse(file, {    complete: function(results) {        console.log("Finished:", results.data);     }   });}

However, one downside is you won't get any type definitions of papaparse


Install papaparse and @types/papaparse. This would enable type definitions for IDE intellisense.

Then, you could import entire papaparse module

import * as Papa from 'papaparse'

or specify required imports as

import { parse } from 'papaparse'

This approach is recommended over third-party wrappers, as they are not updated as frequently or have limited community effort behind them.