Release of jsonlite 0.9.4
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 the jsonlite package was released to CRAN. In addition to adding small new features, this release cleans up code and documentation. Some annoying compiler warnings inherited from RJSONIO
are fixed and the reference manual is a bit more concise. Also some new examples of public JSON APIs were added to the package vignette. These are great to see the power of jsonlite
in action when working with real world JSON structures.
What is jsonlite again?
The jsonlite
package is a fork of RJSONIO
. It builds on the same libjson c++ parser (although a more recent version), but implements a different system for converting between R objects and JSON structures. The most powerful feature is the option to automatically convert tabular JSON structures into R data frames and vice versa. Tabular structures are very common in JSON
data, but usually difficult to read and manipulate. By automatically turning these into data frames jsonlite
can save you many hours and bugs in getting your JSON
data in and out of R. This blog post has some nice examples with data from the Github API.
New in this release
Two new functions were introduced in this release. The minify
function is the opposite of prettify
, and reduces the size of a JSON
blob by removing all redundant whitespace.
The new unbox
function was requested several users. It can be used to force atomic vectors of length 1 to be encoded as a JSON
scalar rather than an array. To understand why this should not be default behavior, see the vignette or this github issue. However it can be useful to do this for individual object elements:
> cat(toJSON(list(foo=123))) { "foo" : [ 123 ] } > cat(toJSON(list(foo=unbox(123)))) { "foo" : 123 }
In the context of a script or function, the unbox
function should only be used for elements that are always exactly length 1, otherwise unbox
will throw an error. This is to protect you from writing code that generates inconsistent JSON
i.e. an array one time and a scalar another time.
The same unbox
function can be used for data frames with exactly 1 row:
> mycar <- cars[23,] > cat(toJSON(mycar)) [ { "speed" : 14, "dist" : 80 } ] > cat(toJSON(unbox(mycar))) { "speed" : 14, "dist" : 80 }
But again this should be used sparsely and with care. When in doubt, always stick with the default toJSON
encodings.
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.