blob: 6ace57e6d3db989256d0955683d6bd845ad6ed39 [file] [log] [blame]
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Reading JSON files &mdash; Apache Arrow v3.0.0</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/theme_overrides.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="canonical" href="https://arrow.apache.org/docs/python/json.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Reading and Writing the Apache Parquet Format" href="parquet.html" />
<link rel="prev" title="Feather File Format" href="feather.html" />
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '20']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Apache Arrow
</a>
<div class="version">
3.0.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Specifications and Protocols</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../format/Versioning.html">Format Versioning and Stability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Columnar.html">Arrow Columnar Format</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Flight.html">Arrow Flight RPC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Integration.html">Integration Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/CDataInterface.html">The Arrow C data interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/CStreamInterface.html">The Arrow C stream interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Other.html">Other Data Structures</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../status.html">Implementation Status</a></li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/c_glib/">C/GLib</a></li>
<li class="toctree-l1"><a class="reference internal" href="../cpp/index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/csharp/README.md">C#</a></li>
<li class="toctree-l1"><a class="reference external" href="https://godoc.org/github.com/apache/arrow/go/arrow">Go</a></li>
<li class="toctree-l1"><a class="reference internal" href="../java/index.html">Java</a></li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/js/">JavaScript</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/julia/Arrow/README.md">Julia</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/matlab/README.md">MATLAB</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Python</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="install.html">Installing PyArrow</a></li>
<li class="toctree-l2"><a class="reference internal" href="memory.html">Memory and IO Interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="data.html">Data Types and In-Memory Data Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="compute.html">Compute Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="ipc.html">Streaming, Serialization, and IPC</a></li>
<li class="toctree-l2"><a class="reference internal" href="filesystems.html">Filesystem Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="filesystems_deprecated.html">Filesystem Interface (legacy)</a></li>
<li class="toctree-l2"><a class="reference internal" href="plasma.html">The Plasma In-Memory Object Store</a></li>
<li class="toctree-l2"><a class="reference internal" href="numpy.html">NumPy Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="pandas.html">Pandas Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="timestamps.html">Timestamps</a></li>
<li class="toctree-l2"><a class="reference internal" href="csv.html">Reading CSV files</a></li>
<li class="toctree-l2"><a class="reference internal" href="feather.html">Feather File Format</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Reading JSON files</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#usage">Usage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#automatic-type-inference">Automatic Type Inference</a></li>
<li class="toctree-l3"><a class="reference internal" href="#customized-parsing">Customized parsing</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="parquet.html">Reading and Writing the Apache Parquet Format</a></li>
<li class="toctree-l2"><a class="reference internal" href="dataset.html">Tabular Datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="cuda.html">CUDA Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="extending_types.html">Extending pyarrow</a></li>
<li class="toctree-l2"><a class="reference internal" href="extending.html">Using pyarrow from C++ and Cython Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html">API Reference</a></li>
<li class="toctree-l2"><a class="reference internal" href="getting_involved.html">Getting Involved</a></li>
<li class="toctree-l2"><a class="reference internal" href="benchmarks.html">Benchmarks</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/r/">R</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/ruby/README.md">Ruby</a></li>
<li class="toctree-l1"><a class="reference external" href="https://docs.rs/crate/arrow/">Rust</a></li>
</ul>
<p class="caption"><span class="caption-text">Development</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../developers/contributing.html">Contributing to Apache Arrow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/cpp/index.html">C++ Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/python.html">Python Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/archery.html">Daily Development using Archery</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/crossbow.html">Packaging and Testing with Crossbow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/docker.html">Running Docker Builds</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/benchmarks.html">Benchmarks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/documentation.html">Building the Documentation</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Apache Arrow</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li><a href="index.html">Python bindings</a> &raquo;</li>
<li>Reading JSON files</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/python/json.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="reading-json-files">
<span id="json"></span><h1>Reading JSON files<a class="headerlink" href="#reading-json-files" title="Permalink to this headline"></a></h1>
<p>Arrow supports reading columnar data from line-delimited JSON files.
In this context, a JSON file consists of multiple JSON objects, one per line,
representing individual data rows. For example, this file represents
two rows of data with four columns “a”, “b”, “c”, “d”:</p>
<div class="highlight-json notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="nt">&quot;a&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="nt">&quot;b&quot;</span><span class="p">:</span> <span class="mf">2.0</span><span class="p">,</span> <span class="nt">&quot;c&quot;</span><span class="p">:</span> <span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="nt">&quot;d&quot;</span><span class="p">:</span> <span class="kc">false</span><span class="p">}</span>
<span class="p">{</span><span class="nt">&quot;a&quot;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="nt">&quot;b&quot;</span><span class="p">:</span> <span class="mf">-5.5</span><span class="p">,</span> <span class="nt">&quot;c&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span> <span class="nt">&quot;d&quot;</span><span class="p">:</span> <span class="kc">true</span><span class="p">}</span>
</pre></div>
</div>
<p>The features currently offered are the following:</p>
<ul class="simple">
<li><p>multi-threaded or single-threaded reading</p></li>
<li><p>automatic decompression of input files (based on the filename extension,
such as <code class="docutils literal notranslate"><span class="pre">my_data.json.gz</span></code>)</p></li>
<li><p>sophisticated type inference (see below)</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Currently only the line-delimited JSON format is supported.</p>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline"></a></h2>
<p>JSON reading functionality is available through the <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyarrow.json</span></code> module.
In many cases, you will simply call the <a class="reference internal" href="generated/pyarrow.json.read_json.html#pyarrow.json.read_json" title="pyarrow.json.read_json"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_json()</span></code></a> function
with the file path you want to read from:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pyarrow</span> <span class="k">import</span> <span class="n">json</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn</span> <span class="o">=</span> <span class="s1">&#39;my_data.json&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span>
<span class="go">pyarrow.Table</span>
<span class="go">a: int64</span>
<span class="go">b: double</span>
<span class="go">c: string</span>
<span class="go">d: bool</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">to_pandas</span><span class="p">()</span>
<span class="go"> a b c d</span>
<span class="go">0 1 2.0 foo False</span>
<span class="go">1 4 -5.5 None True</span>
</pre></div>
</div>
</div>
<div class="section" id="automatic-type-inference">
<h2>Automatic Type Inference<a class="headerlink" href="#automatic-type-inference" title="Permalink to this headline"></a></h2>
<p>Arrow <a class="reference internal" href="data.html#data-types"><span class="std std-ref">data types</span></a> are inferred from the JSON types and
values of each column:</p>
<ul class="simple">
<li><p>JSON null values convert to the <code class="docutils literal notranslate"><span class="pre">null</span></code> type, but can fall back to any
other type.</p></li>
<li><p>JSON booleans convert to <code class="docutils literal notranslate"><span class="pre">bool_</span></code>.</p></li>
<li><p>JSON numbers convert to <code class="docutils literal notranslate"><span class="pre">int64</span></code>, falling back to <code class="docutils literal notranslate"><span class="pre">float64</span></code> if a
non-integer is encountered.</p></li>
<li><p>JSON strings of the kind “YYYY-MM-DD” and “YYYY-MM-DD hh:mm:ss” convert
to <code class="docutils literal notranslate"><span class="pre">timestamp[s]</span></code>, falling back to <code class="docutils literal notranslate"><span class="pre">utf8</span></code> if a conversion error occurs.</p></li>
<li><p>JSON arrays convert to a <code class="docutils literal notranslate"><span class="pre">list</span></code> type, and inference proceeds recursively
on the JSON arrays’ values.</p></li>
<li><p>Nested JSON objects convert to a <code class="docutils literal notranslate"><span class="pre">struct</span></code> type, and inference proceeds
recursively on the JSON objects’ values.</p></li>
</ul>
<p>Thus, reading this JSON file:</p>
<div class="highlight-json notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="nt">&quot;a&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="nt">&quot;b&quot;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&quot;c&quot;</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nt">&quot;d&quot;</span><span class="p">:</span> <span class="s2">&quot;1991-02-03&quot;</span><span class="p">}}</span>
<span class="p">{</span><span class="nt">&quot;a&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="nt">&quot;b&quot;</span><span class="p">:</span> <span class="p">{</span><span class="nt">&quot;c&quot;</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span> <span class="nt">&quot;d&quot;</span><span class="p">:</span> <span class="s2">&quot;2019-04-01&quot;</span><span class="p">}}</span>
</pre></div>
</div>
<p>returns the following data:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="s2">&quot;my_data.json&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span>
<span class="go">pyarrow.Table</span>
<span class="go">a: list&lt;item: int64&gt;</span>
<span class="go"> child 0, item: int64</span>
<span class="go">b: struct&lt;c: bool, d: timestamp[s]&gt;</span>
<span class="go"> child 0, c: bool</span>
<span class="go"> child 1, d: timestamp[s]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span><span class="o">.</span><span class="n">to_pandas</span><span class="p">()</span>
<span class="go"> a b</span>
<span class="go">0 [1, 2] {&#39;c&#39;: True, &#39;d&#39;: 1991-02-03 00:00:00}</span>
<span class="go">1 [3, 4, 5] {&#39;c&#39;: False, &#39;d&#39;: 2019-04-01 00:00:00}</span>
</pre></div>
</div>
</div>
<div class="section" id="customized-parsing">
<h2>Customized parsing<a class="headerlink" href="#customized-parsing" title="Permalink to this headline"></a></h2>
<p>To alter the default parsing settings in case of reading JSON files with an
unusual structure, you should create a <a class="reference internal" href="generated/pyarrow.json.ParseOptions.html#pyarrow.json.ParseOptions" title="pyarrow.json.ParseOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ParseOptions</span></code></a> instance
and pass it to <a class="reference internal" href="generated/pyarrow.json.read_json.html#pyarrow.json.read_json" title="pyarrow.json.read_json"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_json()</span></code></a>. For example, you can pass an explicit
<a class="reference internal" href="data.html#data-schema"><span class="std std-ref">schema</span></a> in order to bypass automatic type inference.</p>
<p>Similarly, you can choose performance settings by passing a
<a class="reference internal" href="generated/pyarrow.json.ReadOptions.html#pyarrow.json.ReadOptions" title="pyarrow.json.ReadOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadOptions</span></code></a> instance to <a class="reference internal" href="generated/pyarrow.json.read_json.html#pyarrow.json.read_json" title="pyarrow.json.read_json"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_json()</span></code></a>.</p>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="parquet.html" class="btn btn-neutral float-right" title="Reading and Writing the Apache Parquet Format" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
<a href="feather.html" class="btn btn-neutral float-left" title="Feather File Format" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&#169; Copyright 2016-2019 Apache Software Foundation.
</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<script type="text/javascript" src="/docs/_static/versionwarning.js"></script></body>
</html>