<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/update-jsonlite-1-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/technotes/2017/01/04/jsonlite-12"> rOpenSci Blog - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A new version of <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/jsonlite/index.html">jsonlite</a> package to CRAN. This is a maintenance release with enhancements and bug fixes. A summary of changes in v1.2 from the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/jsonlite/NEWS">NEWS</a> file:</p>
<ul>
<li>Add <code>read_json</code> and <code>write_json</code> convenience wrappers, <a target="_blank" rel="nofollow" href="https://github.com/jeroenooms/jsonlite/issues/161">#161</a></li>
<li>Update <code>modp_numtoa</code> from upstream, fixes a rounding issue in <a target="_blank" rel="nofollow" href="https://github.com/jeroenooms/jsonlite/issues/148">#148</a>.</li>
<li>Ensure <code>asJSON.POSIXt</code> does not use sci notation for negative values, <a target="_blank" rel="nofollow" href="https://github.com/jeroenooms/jsonlite/issues/155">#155</a></li>
<li>Tweak <code>num_to_char</code> to properly print large negative numbers</li>
<li>Performance optimization for simplyfing data frames (see below)</li>
</ul>
<p>Use the <em>Github compare</em> page to see the full diff on <a target="_blank" rel="nofollow" href="https://github.com/cran/jsonlite/compare/1.1...1.2">metacran</a>.</p>
<h2>New read/write API</h2>
<p>The package has gained new high level functions <code>read_json</code> and <code>write_json</code>. These are <a target="_blank" rel="nofollow" href="https://github.com/cran/jsonlite/blob/1.2/R/read_json.R#L18-L29">wrappers</a> for <code>fromJSON</code> and <code>toJSON</code> which read/write json directly from/to disk. This API is consistent with tidyverse packages like readr, readxl and haven (see <a target="_blank" rel="nofollow" href="https://github.com/jeroenooms/jsonlite/issues/161">#161</a>).</p>
<p>The only thing to note is that <code>read_json</code> does not simplify by default, as is done by <code>fromJSON</code>. For example:</p>
<div>
<pre><code data-lang="r"><span></span><span># Write Data frame to a temp file</span>
tmp <span>&lt;-</span> <span>tempfile</span><span>()</span>
write_json<span>(</span>iris<span>,</span> tmp<span>)</span>

<span># Nested lists</span>
read_json<span>(</span>tmp<span>)</span>

<span># A data frame</span>
read_json<span>(</span>tmp<span>,</span> simplifyVector <span>=</span> <span>TRUE</span><span>)</span>
</code></pre>
</div>
<p>Notice how <code>read_json</code> only returns a data frame when <code>simplifyVector</code> is explicitly set to <code>TRUE</code>.</p>
<h2>Performance enhancements</h2>
<p>We have ported a bit of C code to optimize simplification for data frame structures. This script compares performance for both versions:</p>
<div>
<pre><code data-lang="r"><span></span><span># example json</span>
json <span>&lt;-</span> jsonlite<span>::</span>toJSON<span>(</span>ggplot2<span>::</span>diamonds<span>)</span>

<span># Test with jsonlite 1.1</span>
devtools<span>::</span>install_github<span>(</span><span>&quot;cran/jsonlite@1.1&quot;</span><span>)</span>
microbenchmark<span>::</span>microbenchmark<span>(</span>jsonlite<span>::</span>fromJSON<span>(</span>json<span>),</span> times <span>=</span> <span>50</span><span>)</span>

<span># Unload jsonlite 1.1 (might need restart R on windows)</span>
<span>unloadNamespace</span><span>(</span><span>&quot;jsonlite&quot;</span><span>)</span>
<span>library.dynam.unload</span><span>(</span><span>'jsonlite'</span><span>,</span> <span>find.package</span><span>(</span><span>'jsonlite'</span><span>))</span>

<span># Test with jsonlite 1.2</span>
devtools<span>::</span>install_github<span>(</span><span>&quot;cran/jsonlite@1.2&quot;</span><span>)</span>
microbenchmark<span>::</span>microbenchmark<span>(</span>jsonlite<span>::</span>fromJSON<span>(</span>json<span>),</span> times <span>=</span> <span>50</span><span>)</span>
</code></pre>
</div>
<p>On my Macbook this has reduced the median time from approx 0.91s to 0.76s.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/technotes/2017/01/04/jsonlite-12"> rOpenSci Blog - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SKPF4M39CQE"></a></div>