Site icon R-bloggers

Update jsonlite 1.2

[This article was first published on rOpenSci Blog - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A new version of jsonlite package to CRAN. This is a maintenance release with enhancements and bug fixes. A summary of changes in v1.2 from the NEWS file:

Use the Github compare page to see the full diff on metacran.

New read/write API

The package has gained new high level functions read_json and write_json. These are wrappers for fromJSON and toJSON which read/write json directly from/to disk. This API is consistent with tidyverse packages like readr, readxl and haven (see #161).

The only thing to note is that read_json does not simplify by default, as is done by fromJSON. For example:

# Write Data frame to a temp file
tmp <- tempfile()
write_json(iris, tmp)

# Nested lists
read_json(tmp)

# A data frame
read_json(tmp, simplifyVector = TRUE)

Notice how read_json only returns a data frame when simplifyVector is explicitly set to TRUE.

Performance enhancements

We have ported a bit of C code to optimize simplification for data frame structures. This script compares performance for both versions:

# example json
json <- jsonlite::toJSON(ggplot2::diamonds)

# Test with jsonlite 1.1
devtools::install_github("cran/jsonlite@1.1")
microbenchmark::microbenchmark(jsonlite::fromJSON(json), times = 50)

# Unload jsonlite 1.1 (might need restart R on windows)
unloadNamespace("jsonlite")
library.dynam.unload('jsonlite', find.package('jsonlite'))

# Test with jsonlite 1.2
devtools::install_github("cran/jsonlite@1.2")
microbenchmark::microbenchmark(jsonlite::fromJSON(json), times = 50)

On my Macbook this has reduced the median time from approx 0.91s to 0.76s.

To leave a comment for the author, please follow the link and comment on their blog: rOpenSci Blog - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.