Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

readJSONStream

Read JSON from a Connection/Stream


Description

This function is capable of reading and processing JSON content from a "stream". This is most likely to be from an R connection, but can be an arbitrary source of JSON content. The idea is that the parser will pull partial data from the source and process it immediately, and then return to retrieve more data. This allows the parser to work on the JSON content without it all being in memory at one time. This can save a significant amount of memory and make some computations feasible which would not be if we had to first read all of the JSON and then process it.

Usage

readJSONStream(con, cb = NULL, simplify = Strict, nullValue = NULL,
                 simplifyWithNames = TRUE)

Arguments

con

a connection object from which we will read the JSON content. This can also be any R expression that returns a string. This allows a caller to get content from any source, not just a connection.

cb

an optional callback function that is invoked for each top-level JSON object in the stream. Typically there will only be one such top-level object and so the callback is not really needed as the default is to return that top-level object from readJSONStream. However, if there are multiple top-level JSON objects in the stream, this callback function can process them, e.g. merge them, collapse the contents.

simplify

same as for fromJSON.

nullValue

same as for fromJSON.

simplifyWithNames

same as for fromJSON.

Value

By default, this returns the top-level JSON object in the stream.

Author(s)

Duncan Temple Lang

References

libjson and the JSONSTREAM facilities.

See Also

fromJSON and its methods, specifically the method for a connection.

Examples

## Not run: 
xx = '[1,2, 3]{"a": [true, false]}'
con = textConnection(xx)

f = function(x)
       print(sum(unlist(x)))

readJSONStream(con, f)

  # The callback function can be anonymous
con = textConnection(xx)
readJSONStream(con, function(x)
                       print(sum(unlist(x))))



gen = 
function() {
 ans <- 0
 list(update = function(x) ans <<- ans + sum(unlist(x)),
      value = function() ans)
}
g = gen()
con = textConnection(xx)
readJSONStream(con, g$update)

## End(Not run)

RJSONIO

Serialize R Objects to JSON, JavaScript Object Notation

v1.3-1.4
BSD_3_clause + file LICENSE
Authors
CRAN Team [ctb, cre] (de facto maintainer since 2014), Duncan Temple Lang [aut] (<https://orcid.org/0000-0003-0159-1546>), Jonathan Wallace [aut] (aka ninja9578, author of included libjson sources)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.