PapaParse
Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input
Top Related Projects
📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.
Quick Overview
PapaParse is a powerful, in-browser CSV parser for JavaScript. It's fast, flexible, and capable of parsing large files without crashing the browser. PapaParse can handle various CSV formats and supports streaming, making it ideal for processing large datasets client-side.
Pros
- Fast and efficient parsing of CSV data
- Supports streaming for handling large files
- Works in both browser and Node.js environments
- Highly configurable with many options for customization
Cons
- Limited to CSV format; doesn't support other data formats
- May require additional setup for more complex parsing scenarios
- Documentation could be more comprehensive for advanced use cases
- Performance may vary depending on the browser and file size
Code Examples
Parsing a CSV string:
const csv = "Name,Age\nJohn,30\nJane,25";
const result = Papa.parse(csv);
console.log(result.data);
// Output: [["Name", "Age"], ["John", "30"], ["Jane", "25"]]
Parsing a CSV file:
Papa.parse(file, {
complete: function(results) {
console.log("Finished:", results.data);
}
});
Streaming a large CSV file:
Papa.parse(bigFile, {
worker: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
Getting Started
To use PapaParse in your project, you can include it via CDN or install it using npm:
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
Or install via npm:
npm install papaparse
Then, in your JavaScript code:
import Papa from 'papaparse';
const csv = "Name,Age\nJohn,30\nJane,25";
const result = Papa.parse(csv);
console.log(result.data);
This will parse the CSV string and log the resulting data array to the console.
Competitor Comparisons
📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
Pros of SheetJS
- Supports a wide range of spreadsheet formats (XLS, XLSX, CSV, etc.)
- Handles complex spreadsheet features like formulas and cell styles
- Offers both reading and writing capabilities for spreadsheets
Cons of SheetJS
- Larger file size and potentially slower performance
- Steeper learning curve due to more complex API
- Some features require a commercial license
Code Comparison
SheetJS:
const XLSX = require('xlsx');
const workbook = XLSX.readFile('data.xlsx');
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const data = XLSX.utils.sheet_to_json(sheet);
PapaParse:
const Papa = require('papaparse');
const fs = require('fs');
const file = fs.readFileSync('data.csv', 'utf8');
Papa.parse(file, {
complete: (results) => console.log(results.data)
});
SheetJS offers more comprehensive spreadsheet handling but with added complexity, while PapaParse focuses on simplicity and performance for CSV parsing. SheetJS is better suited for applications requiring advanced spreadsheet manipulation, whereas PapaParse excels in straightforward CSV processing tasks.
Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
Pros of csv-parser
- Faster parsing speed, especially for large CSV files
- Streaming capabilities, allowing processing of data as it's read
- Lightweight with minimal dependencies
Cons of csv-parser
- Less feature-rich compared to PapaParse
- Limited browser support (primarily designed for Node.js)
- Fewer options for customizing parsing behavior
Code Comparison
csv-parser:
const csv = require('csv-parser')
const fs = require('fs')
fs.createReadStream('input.csv')
.pipe(csv())
.on('data', (row) => {
console.log(row)
})
PapaParse:
const Papa = require('papaparse')
const fs = require('fs')
const file = fs.createReadStream('input.csv')
Papa.parse(file, {
complete: (results) => {
console.log(results.data)
}
})
Both libraries offer straightforward ways to parse CSV files, but csv-parser's approach is more stream-oriented, while PapaParse provides a callback-based interface. csv-parser is generally preferred for Node.js applications dealing with large files or streaming data, while PapaParse offers more flexibility and browser compatibility, making it suitable for a wider range of use cases.
A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.
Pros of CSV.js
- Lightweight and simple to use
- Supports both parsing and generating CSV data
- Provides a streaming API for handling large files
Cons of CSV.js
- Less actively maintained compared to PapaParse
- Fewer advanced features and configuration options
- Smaller community and ecosystem
Code Comparison
CSV.js:
const parser = new CSV(options);
parser.fromString(csvString, (err, result) => {
console.log(result);
});
PapaParse:
Papa.parse(csvString, {
complete: (results) => {
console.log(results.data);
}
});
Both libraries offer straightforward APIs for parsing CSV data. CSV.js uses a callback-based approach, while PapaParse provides a configuration object with a complete
callback. PapaParse's API is generally considered more flexible and feature-rich.
CSV.js is a good choice for simple CSV parsing and generation tasks, especially when working with streams. However, PapaParse offers more robust features, better documentation, and a larger community, making it a more suitable option for complex CSV processing needs and projects requiring long-term maintenance.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Parse CSV with JavaScript
Papa Parse is the fastest in-browser CSV (or delimited text) parser for JavaScript. It is reliable and correct according to RFC 4180, and it comes with these features:
- Easy to use
- Parse CSV files directly (local or over the network)
- Fast mode
- Stream large files (even via HTTP)
- Reverse parsing (converts JSON to CSV)
- Auto-detect delimiter
- Worker threads to keep your web page reactive
- Header row support
- Pause, resume, abort
- Can convert numbers and booleans to their types
- Optional jQuery integration to get files from
<input type="file">
elements - One of the only parsers that correctly handles line-breaks and quotations
Papa Parse has no dependencies - not even jQuery.
Install
papaparse is available on npm. It can be installed with the following command:
npm install papaparse
If you don't want to use npm, papaparse.min.js can be downloaded to your project source.
Usage
import Papa from 'papaparse';
Papa.parse(file, config);
const csv = Papa.unparse(data[, config]);
Homepage & Demo
To learn how to use Papa Parse:
The website is hosted on Github Pages. Its content is also included in the docs folder of this repository. If you want to contribute on it just clone the master of this repository and open a pull request.
Papa Parse for Node
Papa Parse can parse a Readable Stream instead of a File when used in Node.js environments (in addition to plain strings). In this mode, encoding
must, if specified, be a Node-supported character encoding. The Papa.LocalChunkSize
, Papa.RemoteChunkSize
, download
, withCredentials
and worker
config options are unavailable.
Papa Parse can also parse in a node streaming style which makes .pipe
available. Simply pipe the Readable Stream to the stream returned from Papa.parse(Papa.NODE_STREAM_INPUT, options)
. The Papa.LocalChunkSize
, Papa.RemoteChunkSize
, download
, withCredentials
, worker
, step
, and complete
config options are unavailable. To register a callback with the stream to process data, use the data
event like so: stream.on('data', callback)
and to signal the end of stream, use the 'end' event like so: stream.on('end', callback)
.
Get Started
For usage instructions, see the homepage and, for more detail, the documentation.
Tests
Papa Parse is under test. Download this repository, run npm install
, then npm test
to run the tests.
Contributing
To discuss a new feature or ask a question, open an issue. To fix a bug, submit a pull request to be credited with the contributors! Remember, a pull request, with test, is best. You may also discuss on Twitter with #PapaParse or directly to me, @mholt6.
If you contribute a patch, ensure the tests suite is running correctly. We run continuous integration on each pull request and will not accept a patch that breaks the tests.
Top Related Projects
📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot