Member-only story

CSV Processing Papa Parse

Eric Fossas
3 min readMay 14, 2018

Papa Parse can convert CSV, TSV, or delimited separated value file into javascript arrays or objects. Now you can work with /.*SV/ files client-side or server-side for web applications.

Using Papa Parse

To use Papa Parse, import the following file:

<script src="https://cdn.rawgit.com/mholt/PapaParse/2f1eb8c4/papaparse.min.js"><script>

To parse a CSV or other separated value file, you just pass in the delimited string into Papa.parse(). The options are optional, although I show some of the important ones in the example below.

// our dummy CSV let csvStr = `ONE,TWO,THREE,FOUR,FIVE "escape""quote","escape,comma",no quotes,"double""""quote","quote"",comma" true,123,null,undefined,"[false,456,null]" true,123,null,undefined,{"one":"two"}`; // running Papa Parse, you just pass it your csv. 
let csv = Papa.parse(csvStr,{
delimiter: "", // auto-detect
newline: "", // auto-detect
quoteChar: '"',
escapeChar: '"',
header: false, // creates array of {head:value}
dynamicTyping: false, // convert values to numbers if possible
skipEmptyLines: true
});
// the arrays of csv fields are in the data property console.log(csv.data);

The New Transform Function

I added a new transform configuration option to Papa Parse. It accepts a function which will receive every value from the CSV, so you can apply any transformations you want.

Below is an example where a transform function attempts to convert CSV fields to javascript values using JSON.parse().

Papa Parse

--

--

No responses yet

Write a response