<li><ahref="#jquery">Using jQuery to select files</a></li>
</ul>
</li>
<li><ahref="#json-to-csv">Convert JSON to CSV</a></li>
<li><ahref="#config">Config</a></li>
<li>
<ahref="#results">Results</a>
<ul>
<li><ahref="#data">Data</a></li>
<li><ahref="#errors">Errors</a></li>
<li><ahref="#meta">Meta</a></li>
</ul>
</li>
<li><ahref="#extras">Extras</a></li>
</ol>
</div>
</div>
<section>
<divclass="grid-container">
<divclass="grid-100">
<h4id="csv-to-json">Convert CSV to JSON</h4>
<p>
Delimited data can be parsed out of strings or files. Files that are parsed can be local or remote. Local files are opened with FileReader, and remote files are downloaded with XMLHttpRequest.
to alter the flow of parsing. Actions can be <code>"abort"</code> to skip this and all other files in the queue, <code>"skip"</code> to skip just this file, or <code>"continue"</code> to carry on (equivalent to returning nothing). <code>reason</code> can be a reason for aborting. <code>config</code> can be a modified <ahref="#config">configuration</a> for parsing just this file.</li>
</li>
<li>The <code>complete</code> callback shown here is executed after <i>all</i> files are finished and does not receive any data. Use the complete callback in <ahref="#config">config</a> for per-file results.</li>
</ul>
</div>
<divclass="clear"></div>
</div>
</section>
<section>
<divclass="grid-container">
<divclass="grid-100">
<h4id="json-to-csv">Convert JSON to CSV</h4>
<p>
Papa's <code>unparse</code> utility writes out correct delimited text strings given an array of arrays or an array of objects.
Set <code>quotes</code> to <code>true</code> to always enclose each field in quotes, or an array of true/false values correlating to specific to columns to force-quote. The character used to quote can be customized using <code>quoteChar</code>. The character used to escape the <code>quoteChar</code> within a field can be customized using <code>escapeChar</code>. The <code>delimiter</code> can be any valid delimiting character. The <code>newline</code> character(s) may also be customized. Setting <code>header</code> to <code>false</code> will omit the header row.
<pre><codeclass="language-javascript">// With implicit header row
// (keys of first object populate header row)
var csv = Papa.unparse([
{
"Column 1": "foo",
"Column 2": "bar"
},
{
"Column 1": "abc",
"Column 2": "def"
}
]);</code></pre>
</div>
<divclass="grid-33">
<pre><codeclass="language-javascript">// Specifying fields and data explicitly
var csv = Papa.unparse({
fields: ["Column 1", "Column 2"],
data: [
["foo", "bar"],
["abc", "def"]
]
});</code></pre>
</div>
<divclass="clear"></div>
</div>
</section>
<section>
<divclass="grid-container">
<divclass="grid-100">
<h4id="config">The Parse Config Object</h4>
<p>
The <code>parse</code> function may be passed a configuration object. It defines settings, behavior, and callbacks used during parsing. Any properties left unspecified will resort to their default values.
</p>
</div>
<divclass="grid-100">
<h5id="config-default">Default Config With All Options</h5>
</div>
<divclass="prefix-25 grid-50 suffix-25">
<pre><codeclass="language-javascript">{
delimiter: "", // auto-detect
newline: "", // auto-detect
quoteChar: '"',
escapeChar: '"',
header: false,
trimHeaders: false,
dynamicTyping: false,
preview: 0,
encoding: "",
worker: false,
comments: false,
step: undefined,
complete: undefined,
error: undefined,
download: false,
skipEmptyLines: false,
chunk: undefined,
fastMode: undefined,
beforeFirstChunk: undefined,
withCredentials: undefined,
transform: undefined
}</code></pre>
</div>
<divclass="clear"></div>
<divclass="grid-100">
<h5id="config-details">Config Options</h5>
</div>
<divclass="grid-100"style="overflow-x: auto;">
<table>
<tr>
<thstyle="width: 20%;">Option</th>
<thstyle="width: 80%;">Explanation</th>
</tr>
<tr>
<td>
<code>delimiter</code>
</td>
<td>
The delimiting character. Leave blank to auto-detect from a list of most common delimiters. It can be a string or a function. If string, it must be one of length 1. If a function, it must accept the input as first parameter and it must return a string which will be used as delimiter. In both cases it cannot be found in <ahref="#readonly">Papa.BAD_DELIMITERS</a>.
</td>
</tr>
<tr>
<td>
<code>newline</code>
</td>
<td>
The newline sequence. Leave blank to auto-detect. Must be one of \r, \n, or \r\n.
</td>
</tr>
<tr>
<td>
<code>quoteChar</code>
</td>
<td>
The character used to quote fields. The quoting of all fields is not mandatory. Any field which is not quoted will correctly read.
</td>
</tr>
<tr>
<td>
<code>escapeChar</code>
</td>
<td>
The character used to escape the quote character within a field. If not set, this option will default to the value of <code>quoteChar</code>, meaning that the default escaping of quote character within a quoted field is using the quote character two times. (e.g. <code>"column with ""quotes"" in text"</code>)
</td>
</tr>
<tr>
<td>
<code>header</code>
</td>
<td>
If true, the first row of parsed data will be interpreted as field names. An array of field names will be returned in <ahref="#meta">meta</a>, and each row of data will be an object of values keyed by field name instead of a simple array. Rows with a different number of fields from the header row will produce an error. Warning: Duplicate field names will overwrite values in previous fields having the same name.
</td>
</tr>
<tr>
<td>
<code>trimHeaders</code>
</td>
<td>
If true leading/trailing spaces will be trimed from headers.
</td>
</tr>
<tr>
<td>
<code>dynamicTyping</code>
</td>
<td>
If true, numeric and boolean data will be converted to their type instead of remaining strings. Numeric data must conform to the definition of a decimal literal. European-formatted numbers must have commas and dots swapped. If also accepts an object or a function. If object it's values should be a boolean to indicate if dynamic typing should be applied for each column number (or header name if using headers). If it's a function, it should return a boolean value for each field number (or name if using headers) which will be passed as first argument.
</td>
</tr>
<tr>
<td>
<code>preview</code>
</td>
<td>
If > 0, only that many rows will be parsed.
</td>
</tr>
<tr>
<td>
<code>encoding</code>
</td>
<td>
The encoding to use when opening local files. If specified, it must be a value supported by the FileReader API.
</td>
</tr>
<tr>
<td>
<code>worker</code>
</td>
<td>
Whether or not to use a <ahref="/faq#workers">worker thread</a>. Using a worker will keep your page reactive, but may be slightly slower. Web Workers also load the entire Javascript file, so be careful when <ahref="/faq#combine">combining other libraries</a> in the same file as Papa Parse. Note that worker option is only available when parsing files and not when converting from JSON to CSV.
</td>
</tr>
<tr>
<td>
<code>comments</code>
</td>
<td>
A string that indicates a comment (for example, "#" or "//"). When Papa encounters a line starting with this string, it will skip the line.
</td>
</tr>
<tr>
<td>
<code>step</code>
</td>
<td>
To <ahref="/faq#streaming">stream</a> the input, define a callback function:
Streaming is necessary for large files which would otherwise crash the browser. You can call <code>parser.abort()</code> to abort parsing. And, except when using a <ahref="/faq#worker">Web Worker</a>, you can call <code>parser.pause()</code> to pause it, and <code>parser.resume()</code> to resume.
</td>
</tr>
<tr>
<td>
<code>complete</code>
</td>
<td>
The callback to execute when parsing is complete. It receives the parse <ahref="#results">results</a>. If parsing a local file, the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/File">File</a> is passed in, too:
When streaming, parse results are <i>not</i> available in this callback.
</td>
</tr>
<tr>
<td>
<code>error</code>
</td>
<td>
A callback to execute if FileReader encounters an error. The function is passed two arguments: the error and the File.
</td>
</tr>
<tr>
<td>
<code>download</code>
</td>
<td>
If true, this indicates that the string you passed as the first argument to <code>parse()</code> is actually a URL from which to download a file and parse its contents.
If true, lines that are completely empty (those which evaluate to an empty string) will be skipped. If set to <code>'greedy'</code>, lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
A callback function, identical to step, which activates streaming. However, this function is executed after every <i>chunk</i> of the file is loaded and parsed rather than every row. Works only with local and remote files. Do not use both chunk and step callbacks together. For the function signature, see the documentation for the step function.
</td>
</tr>
<tr>
<td>
<code>fastMode</code>
</td>
<td>
Fast mode speeds up parsing significantly for large inputs. However, it only works when the input has no quoted fields. Fast mode will automatically be enabled if no <code>"</code> characters appear in the input. You can force fast mode either way by setting it to <code>true</code> or <code>false</code>.
</td>
</tr>
<tr>
<td>
<code>beforeFirstChunk</code>
</td>
<td>
A function to execute before parsing the first chunk. Can be used with chunk or step streaming modes. The function receives as an argument the chunk about to be parsed, and it may return a modified chunk to parse. This is useful for stripping header lines (as long as the header fits in a single chunk).
</td>
</tr>
<tr>
<td>
<code>withCredentials</code>
</td>
<td>
A boolean value passed directly into XMLHttpRequest's "withCredentials" property.
</td>
</tr>
<tr>
<td>
<code>transform</code>
</td>
<td>
A function to apply on each value. The function receives the value as its first argument and the column number as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping.
</td>
</tr>
</table>
</div>
</div>
</section>
<section>
<divclass="grid-container">
<divclass="grid-100">
<h4id="results">The Parse Result Object</h4>
<p>
A parse result always contains three objects: data, errors, and meta. Data and errors are arrays, and meta is an object. In the step callback, the data array will only contain one element.
</p>
</div>
<divclass="grid-100">
<h5id="results-structure">Result Structure</h5>
</div>
<divclass="grid-50">
<pre><codeclass="language-javascript">{
data: // array of parsed data
errors: // array of errors
meta: // object with extra info
}</code></pre>
</div>
<divclass="grid-50">
<ul>
<li><code>data</code> is an array of rows. If header is false, rows are arrays; otherwise they are objects of data keyed by the field name.</li>
<li><code>errors</code> is an array of <ahref="#errors">errors</a>.</li>
<li><code>meta</code> contains extra information about the parse, such as delimiter used, the newline sequence, whether the process was aborted, etc. Properties in this object are not guaranteed to exist in all situations.</li>
</ul>
</div>
<divclass="clear"></div>
<divclass="grid-100">
<h5id="data">Data</h5>
</div>
<divclass="grid-50">
<pre><codeclass="language-javascript">// Example (header: false)
[
["Column 1", "Column 2"],
["foo", "bar"],
["abc", "def"]
]
// Example (header: true)
[
{
"Column 1": "foo",
"Column 2": "bar"
},
{
"Column 1": "abc",
"Column 2": "def"
}
]</code></pre>
</div>
<divclass="grid-50">
<ul>
<li>If header row is enabled and more fields are found on a row of data than in the header row, an extra field will appear in that row called <code>__parsed_extra</code>. It contains an array of all data parsed from that row that extended beyond the header row.</li>
row: 0, // Row index of parsed data where error is
<!--index: 0 // Character index within original input-->
}</code></pre>
</div>
<divclass="grid-50">
<ul>
<li>The error <code>type</code> will be one of "Quotes", "Delimiter", or "FieldMismatch".</li>
<li>The <code>code</code> may be "MissingQuotes", "UndetectableDelimiter", "TooFewFields", or "TooManyFields" (depending on the error type).</li>
<!--<li><code>index</code> may not be available on all error messages because some errors are only generated after parsing is already complete.</li>-->
<li>Just because errors are generated does not necessarily mean that parsing failed. The worst error you can get is probably MissingQuotes.</li>
</ul>
</div>
<divclass="clear"></div>
<divclass="grid-100">
<h5id="meta">Meta</h5>
</div>
<divclass="grid-50">
<pre><codeclass="language-javascript">{
delimiter: // Delimiter used
linebreak: // Line break sequence used
aborted: // Whether process was aborted
fields: // Array of field names
truncated: // Whether preview consumed all input
}</code></pre>
</div>
<divclass="grid-50">
<ul>
<li>Not all meta properties will always be available. For instance, <code>fields</code> is only given when header row is enabled.</li>
There's a few other things that Papa exposes to you that weren't explained above.
</p>
</div>
<divclass="grid-100">
<h5id="readonly">Read-Only</h5>
</div>
<divclass="grid-100">
<table>
<tr>
<th>Read-Only Property</th>
<th>Explanation</th>
</tr>
<tr>
<td><code>Papa.BAD_DELIMITERS</code></td>
<td>
An array of characters that are not allowed as delimiters.
</td>
</tr>
<tr>
<td><code>Papa.RECORD_SEP</code></td>
<td>
The true delimiter. Invisible. ASCII code 30. Should be doing the job we strangely rely upon commas and tabs for.
</td>
</tr>
<tr>
<td><code>Papa.UNIT_SEP</code></td>
<td>
Also sometimes used as a delimiting character. ASCII code 31.
</td>
</tr>
<tr>
<td><code>Papa.WORKERS_SUPPORTED</code></td>
<td>
Whether or not the browser supports HTML5 Web Workers. If false, <code>worker: true</code> will have no effect.
</td>
</tr>
<tr>
<td><code>Papa.SCRIPT_PATH</code></td>
<td>
The relative path to Papa Parse. This is automatically detected when Papa Parse is loaded synchronously. However, if you load Papa Parse asynchronously (e.g. with RequireJS), you need to set this variable manually in order to use Web Workers. (In those cases, this variable is <i>not</i> read-only and you should set it!)
</td>
</tr>
</table>
</div>
<divclass="grid-100">
<h5id="configurable">Configurable</h5>
</div>
<divclass="grid-100">
<table>
<tr>
<th>Configurable Property</th>
<th>Explanation</th>
</tr>
<tr>
<td><code>Papa.LocalChunkSize</code></td>
<td>
The size in bytes of each file chunk. Used when streaming files obtained from the DOM that exist on the local computer. Default 10 MB.
</td>
</tr>
<tr>
<td><code>Papa.RemoteChunkSize</code></td>
<td>
Same as LocalChunkSize, but for downloading files from remote locations. Default 5 MB.
</td>
</tr>
<tr>
<td><code>Papa.DefaultDelimiter</code></td>
<td>
The delimiter used when it is left unspecified and cannot be detected automatically. Default is comma.
</td>
</tr>
</table>
</div>
<divclass="clear"></div>
</div>
</section>
</main>
<footer>
<!--<div class="footer-top">
<h3>Make Your Papa Proud</h3>
<h4><ahref="https://github.com/mholt/PapaParse">Star</a> and <ahref="https://github.com/mholt/PapaParse/blob/gh-pages/resources/js/lovers.js">shout</a> if you love #PapaParse</h4>
</div>-->
<divclass="footer-main">
<divclass="grid-container">
<divclass="grid-40 text-center">
<divclass="logo">P</div>
<br><br>
Papa Parse by <ahref="https://twitter.com/mholt6">Matt Holt</a>