Stringify R objects to JSON and vice versa
These functions are used to convert R objects into JSON
and back. The
fromJSON functions use a class based
encoding schema which follows conventions outlines in the
vignette of this package. They implement a consitent and
practical mapping between JSON structures and the
standard data structures in R.
fromJSON(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector, simplifyMatrix = simplifyVector)
toJSON(x, dataframe = c("rows", "columns"), Date = c("ISO8601", "epoch"), POSIXt = c("string", "ISO8601", "epoch", "mongo"), factor = c("string", "integer"), complex = c("string", "list"), raw = c("base64", "hex", "mongo"), na = c("default", "null", "string"), digits = 2, pretty = FALSE, ...)
- the object to be encoded
- how to encode data.frame objects: must be one of "row" or "column"
- how to encode Date objects: must be one of "ISO8601" or "epoch"
- how to encode POSIXt (datetime) objects: must be one of "string", "ISO8601", "epoch" or "mongo"
- how to encode factor objects: must be one of "string" or "integer"
- how to encode complex numbers: must be one of "string" or "list"
- how to encode raw objects: must be one of "base64", "hex" or "mongo"
- max number of digits (after the dot) to print for numeric values
- how to print NA values. One of "default", "null" or "string"
- adds indentation whitespace to JSON output.
- a string in json format
- automatically coerse JSON arrays containing only scalars into a vector
- automatically coerse JSON arrays containing records (JSON objects with scalars) into a data frame.
- automatically coerse JSON arrays with vectors of equal length and mode into a matrix
- arguments passed on to class specific
functions are drop-in replacements for the identically
named functions in packages rjson and RJSONIO. Our
implementation uses an alternative, somewhat more
consistent mapping between R objects and JSON strings.
unserializeJSON functions use an
alternative system to convert between R objects and JSON,
which supports more classes but is much more verbose.
All encoded objects should pass the validation at www.jsonlint.org
#stringify some data jsoncars <- toJSON(mtcars, pretty=TRUE) cat(jsoncars) #parse it back fromJSON(jsoncars)