Papa Parse
A powerful, in-browser CSV parser for big boys and girls
Now the fastest JavaScript CSV parser for the browser
The world's first multi-threaded CSV parser for the browser
Use Papa when performance, privacy, and correctness matter to you
Papa is easy to use:
var results = Papa.parse(csv);
Convert JSON to CSV:
var csv = Papa.unparse(json);
Companies trust Papa to help alleviate privacy concerns related to uploading files.
Malformed CSV is handled gracefully with a detailed error report.
var results = Papa.parse(csv);
var csv = Papa.unparse(json);
Features
- CSV → JSON and JSON → CSV
- Auto-detect delimiter
- Open local files
- Download remote files
- Stream local or remote files
- Multi-threaded
- Header row support
- Number/boolean type conversion
- Skip commented lines
- Fast mode for input without quoted fields
- Gracefully handles malformed input
- Just a sprinkle of jQuery (optional)
// Convert CSV to JSON
var results = Papa.parse(csv);
// Parse local CSV files
$('input[type=file]').parse({
config: {
complete: function(results) {
console.log("Parse results:", results.data);
}
}
});
// In a worker thread
Papa.parse(fileOrString, {
worker: true,
complete: function(results) {
console.log("Parse results:", results.data);
}
});
"I don't know the delimiter..."
That's okay. Papa will scan the first few rows of input to find the right delimiter for you. You can also set the delimiting character manually. Either way, the delimiter used is returned with every result set.
var results = Papa.parse(csvString);
console.log(results);
/*
{
data: [ ... ]
errors: [ ... ]
meta: {
delimiter: "\t",
...
}
}
*/
"Great, but I have a file to parse."
Just give Papa a File instead of a string. And a callback.
Papa.parse(file, {
complete: function(results) {
console.log(results);
}
});
"No, you don't understand. The file isn't local."
Well then just give Papa the URL and—of course—a callback.
Papa.parse("http://example.com/foo.csv", {
download: true,
complete: function(results) {
console.log("Remote file parsed!", results);
}
});
"Did I mention the file is huge?"
That's what streaming is for. Specify a step
callback to receive the results row-by-row. This way, you won't load the whole file into memory and crash the browser.
Papa.parse("http://example.com/bigfoo.csv", {
download: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
"Lovely. Now my web page locked up."
Oh. Yeah, that happens when a long-running script is executing in the same thread. Use a Worker thread by specifying worker: true
. It may take slightly longer, but your page will stay reactive.
Papa.parse(bigFile, {
worker: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
"Great! Now I want data keyed by field name."
You can tell Papa that there is a header row.
// Key data by field name instead of index/position
var results = Papa.parse(csv, {
header: true
});
"Hey, these numbers are all parsed as strings."
Everything is parsed as strings. If you need the convenience, you can have numeric and boolean data automatically converted to number and boolean types.
// All parsed data is normally returned as a string.
// Dynamic typing converts numbers to numbers
// and booleans to booleans.
var results = Papa.parse(csv, {
dynamicTyping: true
});
"I forgot to mention: my CSV files have comments in them."
Okay, first off: that's really weird. But you can skip those lines... just specify the comment character.
// Mostly found in academia, some CSV files
// may have commented lines in them
var results = Papa.parse(csv, {
comments: "#"
});
"I'm getting tired, are we done—aw, shoot. Errors."
Yeah, almost done. Fortunately, Papa handles errors pretty well. The CSV standard is somewhat loose ambiguous, so Papa tries to consider the edge cases. For example, mismatched fields aren't always the end of the world.
// Example error:
{
type: "FieldMismatch",
code: "TooManyFields",
message: "Expected 3 fields, but parsed 4",
row: 1
}
"Can I use Papa with jQuery?"
Sure, but it's not required. You can use jQuery to select file input elements and then parse their files. Papa exposes its file parsing API as a jQuery plugin only when jQuery is defined. Papa Parse has no dependencies.
$("input[type=file]").parse({
config: {
complete: function(results, file) {
console.log("File done:", file, results);
}
},
complete: function() {
console.log("All files done!");
}
});
"Last thing: what about converting JSON to CSV?"
Call unparse()
instead of parse()
, passing in your array of arrays or array of objects. Papa will figure it out.
// Output is a properly-formatted CSV string.
// See the docs for more configurability.
var csv = Papa.unparse(yourData);