<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>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.
If <code>true</code>, forces all fields to be enclosed in quotes. If an array of <code>true/false</code> values, specifies which fields should be force-quoted (first boolean is for the first column, second boolean for the second column, ...). A function that returns a boolean values can be used to determine the quotes value of a cell. This function accepts the cell value and column index as parameters. <br/>
Note that this option is ignored for <code>undefined</code>, <code>null</code> and <code>date-object</code> values. The option <code>escapeFormulae</code> also takes precedence over this.
If <code>false</code>, will omit the header row. If <code>data</code> is an array of arrays this option is ignored. If <code>data</code> is an array of objects the keys of the first object are the header row. If <code>data</code> is an object with the keys <code>fields</code> and <code>data</code> the <code>fields</code> are the header row.
If <code>true</code>, 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.
If <code>data</code> is an array of objects this option can be used to manually specify the keys (columns) you expect in the objects. If not set the keys of the first objects are used as column.
If <code>true</code>, field values that begin with <code>=</code>, <code>+</code>, <code>-</code>, <code>@</code>, <code>\t</code>, or <code>\r</code>, will be prepended with a <code>'</code> to defend against <ahref="https://owasp.org/www-community/attacks/CSV_Injection"target="_blank"rel="noopener">injection attacks</a>, because Excel and LibreOffice will automatically parse such cells as formulae. You can override those values by setting this option to a regular expression
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>
The delimiting character. Leave blank to auto-detect from a list of most common delimiters, or any values passed in through <code>delimitersToGuess</code>. It can be a string or a function. If a string, it can be of any length (so multi-character delimiters are supported). 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>.
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.
A function to apply on each header. Requires <code>header</code> to be <code>true</code>. The function receives the header as its first argument and the index as second.<br>
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. Numerical values greater than <code>2^53</code> or less than <code>-2^53</code> will not be converted to numbers to preserve precision. 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.
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.
Overrides <code>Papa.LocalChunkSize</code> and <code>Papa.RemoteChunkSize</code>. See <ahref="#configurable">configurable</a> section to know the usage of both parameters.
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.
A function to apply on each value. The function receives the value as its first argument and the column number or header name when enabled as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping.
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>
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>
</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>