Papa Parse

A powerful, in-browser CSV parser for big boys and girls

P

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.

Features

// 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); } });
Delimeter auto-detect

"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); /* { data: ... errors: ... meta: { delimiter: "\t", ... } } */

Parse local files

"Great, but I have a file to parse."

Just give Papa a File instead of a string. Oh, and a callback.

Papa.parse(file, { complete: function(results) { console.log(results); } });

Parse remote files

"No, you don't understand. The file isn't local."

Ah. Then 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); } });

Streaming

"Did I mention the file is huge?"

That's what streaming is for. Just 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!"); } });

Multi-threading

"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!"); } });

"Rad! What if I want data keyed by field name?"

All you have to do is tell Papa that there is a header row. This is a convenience, however, which comes at a slight performance cost, negligible for most inputs.

// Key data by field name instead of index/position var results = Papa.parse(csv, { header: true });

Type Conversion

"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 the number and boolean types, respectively.

// 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 });

Comments

"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: "#" });

Error handling

"Are we done yet? I'm—aw, shoot. Errors."

(Almost done!) Fortunately, Papa can handle errors pretty well. The CSV standard is somewhat loose ambiguous, so Papa tries to consider the edge cases. For example, unescaped quotes aren't always the end of the world.

// Errors are returned with results. Always. Example: /* { type: "Quotes", code: "UnexpectedQuotes", message: "Unexpected quotes", line: 2, row: 1, index: 83 } */

jQuery Plugin

"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!"); } });

JSON to CSV

"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);

Who's your Papa?

Mini Papa for production use
Fat Papa for debug and development