R objects to/from JSON — toJSON, fromJSON" />

These functions are used to convert between JSON data and R objects. The toJSON and fromJSON functions use a class based mapping, which follows conventions outlined in this paper: https://arxiv.org/abs/1403.2805 (also available as vignette).

fromJSON(
  txt,
  simplifyVector = TRUE,
  simplifyDataFrame = simplifyVector,
  simplifyMatrix = simplifyVector,
  flatten = FALSE,
  ...
)

toJSON(
  x,
  dataframe = c("rows", "columns", "values"),
  matrix = c("rowmajor", "columnmajor"),
  Date = c("ISO8601", "epoch"),
  POSIXt = c("string", "ISO8601", "epoch", "mongo"),
  factor = c("string", "integer"),
  complex = c("string", "list"),
  raw = c("base64", "hex", "mongo", "int", "js"),
  null = c("list", "null"),
  na = c("null", "string"),
  auto_unbox = FALSE,
  digits = 4,
  pretty = FALSE,
  force = FALSE,
  ...
)

Arguments

txt

a JSON string, URL or file

simplifyVector

coerce JSON arrays containing only primitives into an atomic vector

simplifyDataFrame

coerce JSON arrays containing only records (JSON objects) into a data frame

simplifyMatrix

coerce JSON arrays containing vectors of equal mode and dimension into matrix or array

flatten

automatically flatten nested data frames into a single non-nested data frame

...

arguments passed on to class specific print methods

x

the object to be encoded

dataframe

how to encode data.frame objects: must be one of 'rows', 'columns' or 'values'

matrix

how to encode matrices and higher dimensional arrays: must be one of 'rowmajor' or 'columnmajor'.

Date

how to encode Date objects: must be one of 'ISO8601' or 'epoch'

POSIXt

how to encode POSIXt (datetime) objects: must be one of 'string', 'ISO8601', 'epoch' or 'mongo'

factor

how to encode factor objects: must be one of 'string' or 'integer'

complex

how to encode complex numbers: must be one of 'string' or 'list'

raw

how to encode raw objects: must be one of 'base64', 'hex' or 'mongo'

null

how to encode NULL values within a list: must be one of 'null' or 'list'

na

how to print NA values: must be one of 'null' or 'string'. Defaults are class specific

auto_unbox

automatically unbox all atomic vectors of length 1. It is usually safer to avoid this and instead use the unbox function to unbox individual elements. An exception is that objects of class AsIs (i.e. wrapped in I()) are not automatically unboxed. This is a way to mark single values as length-1 arrays.

digits

max number of decimal digits to print for numeric values. Use I() to specify significant digits. Use NA for max precision.

pretty

adds indentation whitespace to JSON output. Can be TRUE/FALSE or a number specifying the number of spaces to indent. See prettify

force

unclass/skip objects of classes with no defined JSON mapping

Details

The toJSON and fromJSON 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.

The serializeJSON and unserializeJSON functions in this package use an alternative system to convert between R objects and JSON, which supports more classes but is much more verbose.

A JSON string is always unicode, using UTF-8 by default, hence there is usually no need to escape any characters. However, the JSON format does support escaping of unicode characters, which are encoded using a backslash followed by a lower case "u" and 4 hex characters, for example: "Z\u00FCrich". The fromJSON function will parse such escape sequences but it is usually preferable to encode unicode characters in JSON using native UTF-8 rather than escape sequences.

References

Jeroen Ooms (2014). The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R Objects. arXiv:1403.2805. https://arxiv.org/abs/1403.2805

Examples

# Stringify some data jsoncars <- toJSON(mtcars, pretty=TRUE) cat(jsoncars)
#> [ #> { #> "mpg": 21, #> "cyl": 6, #> "disp": 160, #> "hp": 110, #> "drat": 3.9, #> "wt": 2.62, #> "qsec": 16.46, #> "vs": 0, #> "am": 1, #> "gear": 4, #> "carb": 4, #> "_row": "Mazda RX4" #> }, #> { #> "mpg": 21, #> "cyl": 6, #> "disp": 160, #> "hp": 110, #> "drat": 3.9, #> "wt": 2.875, #> "qsec": 17.02, #> "vs": 0, #> "am": 1, #> "gear": 4, #> "carb": 4, #> "_row": "Mazda RX4 Wag" #> }, #> { #> "mpg": 22.8, #> "cyl": 4, #> "disp": 108, #> "hp": 93, #> "drat": 3.85, #> "wt": 2.32, #> "qsec": 18.61, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 1, #> "_row": "Datsun 710" #> }, #> { #> "mpg": 21.4, #> "cyl": 6, #> "disp": 258, #> "hp": 110, #> "drat": 3.08, #> "wt": 3.215, #> "qsec": 19.44, #> "vs": 1, #> "am": 0, #> "gear": 3, #> "carb": 1, #> "_row": "Hornet 4 Drive" #> }, #> { #> "mpg": 18.7, #> "cyl": 8, #> "disp": 360, #> "hp": 175, #> "drat": 3.15, #> "wt": 3.44, #> "qsec": 17.02, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 2, #> "_row": "Hornet Sportabout" #> }, #> { #> "mpg": 18.1, #> "cyl": 6, #> "disp": 225, #> "hp": 105, #> "drat": 2.76, #> "wt": 3.46, #> "qsec": 20.22, #> "vs": 1, #> "am": 0, #> "gear": 3, #> "carb": 1, #> "_row": "Valiant" #> }, #> { #> "mpg": 14.3, #> "cyl": 8, #> "disp": 360, #> "hp": 245, #> "drat": 3.21, #> "wt": 3.57, #> "qsec": 15.84, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 4, #> "_row": "Duster 360" #> }, #> { #> "mpg": 24.4, #> "cyl": 4, #> "disp": 146.7, #> "hp": 62, #> "drat": 3.69, #> "wt": 3.19, #> "qsec": 20, #> "vs": 1, #> "am": 0, #> "gear": 4, #> "carb": 2, #> "_row": "Merc 240D" #> }, #> { #> "mpg": 22.8, #> "cyl": 4, #> "disp": 140.8, #> "hp": 95, #> "drat": 3.92, #> "wt": 3.15, #> "qsec": 22.9, #> "vs": 1, #> "am": 0, #> "gear": 4, #> "carb": 2, #> "_row": "Merc 230" #> }, #> { #> "mpg": 19.2, #> "cyl": 6, #> "disp": 167.6, #> "hp": 123, #> "drat": 3.92, #> "wt": 3.44, #> "qsec": 18.3, #> "vs": 1, #> "am": 0, #> "gear": 4, #> "carb": 4, #> "_row": "Merc 280" #> }, #> { #> "mpg": 17.8, #> "cyl": 6, #> "disp": 167.6, #> "hp": 123, #> "drat": 3.92, #> "wt": 3.44, #> "qsec": 18.9, #> "vs": 1, #> "am": 0, #> "gear": 4, #> "carb": 4, #> "_row": "Merc 280C" #> }, #> { #> "mpg": 16.4, #> "cyl": 8, #> "disp": 275.8, #> "hp": 180, #> "drat": 3.07, #> "wt": 4.07, #> "qsec": 17.4, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 3, #> "_row": "Merc 450SE" #> }, #> { #> "mpg": 17.3, #> "cyl": 8, #> "disp": 275.8, #> "hp": 180, #> "drat": 3.07, #> "wt": 3.73, #> "qsec": 17.6, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 3, #> "_row": "Merc 450SL" #> }, #> { #> "mpg": 15.2, #> "cyl": 8, #> "disp": 275.8, #> "hp": 180, #> "drat": 3.07, #> "wt": 3.78, #> "qsec": 18, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 3, #> "_row": "Merc 450SLC" #> }, #> { #> "mpg": 10.4, #> "cyl": 8, #> "disp": 472, #> "hp": 205, #> "drat": 2.93, #> "wt": 5.25, #> "qsec": 17.98, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 4, #> "_row": "Cadillac Fleetwood" #> }, #> { #> "mpg": 10.4, #> "cyl": 8, #> "disp": 460, #> "hp": 215, #> "drat": 3, #> "wt": 5.424, #> "qsec": 17.82, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 4, #> "_row": "Lincoln Continental" #> }, #> { #> "mpg": 14.7, #> "cyl": 8, #> "disp": 440, #> "hp": 230, #> "drat": 3.23, #> "wt": 5.345, #> "qsec": 17.42, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 4, #> "_row": "Chrysler Imperial" #> }, #> { #> "mpg": 32.4, #> "cyl": 4, #> "disp": 78.7, #> "hp": 66, #> "drat": 4.08, #> "wt": 2.2, #> "qsec": 19.47, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 1, #> "_row": "Fiat 128" #> }, #> { #> "mpg": 30.4, #> "cyl": 4, #> "disp": 75.7, #> "hp": 52, #> "drat": 4.93, #> "wt": 1.615, #> "qsec": 18.52, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 2, #> "_row": "Honda Civic" #> }, #> { #> "mpg": 33.9, #> "cyl": 4, #> "disp": 71.1, #> "hp": 65, #> "drat": 4.22, #> "wt": 1.835, #> "qsec": 19.9, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 1, #> "_row": "Toyota Corolla" #> }, #> { #> "mpg": 21.5, #> "cyl": 4, #> "disp": 120.1, #> "hp": 97, #> "drat": 3.7, #> "wt": 2.465, #> "qsec": 20.01, #> "vs": 1, #> "am": 0, #> "gear": 3, #> "carb": 1, #> "_row": "Toyota Corona" #> }, #> { #> "mpg": 15.5, #> "cyl": 8, #> "disp": 318, #> "hp": 150, #> "drat": 2.76, #> "wt": 3.52, #> "qsec": 16.87, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 2, #> "_row": "Dodge Challenger" #> }, #> { #> "mpg": 15.2, #> "cyl": 8, #> "disp": 304, #> "hp": 150, #> "drat": 3.15, #> "wt": 3.435, #> "qsec": 17.3, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 2, #> "_row": "AMC Javelin" #> }, #> { #> "mpg": 13.3, #> "cyl": 8, #> "disp": 350, #> "hp": 245, #> "drat": 3.73, #> "wt": 3.84, #> "qsec": 15.41, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 4, #> "_row": "Camaro Z28" #> }, #> { #> "mpg": 19.2, #> "cyl": 8, #> "disp": 400, #> "hp": 175, #> "drat": 3.08, #> "wt": 3.845, #> "qsec": 17.05, #> "vs": 0, #> "am": 0, #> "gear": 3, #> "carb": 2, #> "_row": "Pontiac Firebird" #> }, #> { #> "mpg": 27.3, #> "cyl": 4, #> "disp": 79, #> "hp": 66, #> "drat": 4.08, #> "wt": 1.935, #> "qsec": 18.9, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 1, #> "_row": "Fiat X1-9" #> }, #> { #> "mpg": 26, #> "cyl": 4, #> "disp": 120.3, #> "hp": 91, #> "drat": 4.43, #> "wt": 2.14, #> "qsec": 16.7, #> "vs": 0, #> "am": 1, #> "gear": 5, #> "carb": 2, #> "_row": "Porsche 914-2" #> }, #> { #> "mpg": 30.4, #> "cyl": 4, #> "disp": 95.1, #> "hp": 113, #> "drat": 3.77, #> "wt": 1.513, #> "qsec": 16.9, #> "vs": 1, #> "am": 1, #> "gear": 5, #> "carb": 2, #> "_row": "Lotus Europa" #> }, #> { #> "mpg": 15.8, #> "cyl": 8, #> "disp": 351, #> "hp": 264, #> "drat": 4.22, #> "wt": 3.17, #> "qsec": 14.5, #> "vs": 0, #> "am": 1, #> "gear": 5, #> "carb": 4, #> "_row": "Ford Pantera L" #> }, #> { #> "mpg": 19.7, #> "cyl": 6, #> "disp": 145, #> "hp": 175, #> "drat": 3.62, #> "wt": 2.77, #> "qsec": 15.5, #> "vs": 0, #> "am": 1, #> "gear": 5, #> "carb": 6, #> "_row": "Ferrari Dino" #> }, #> { #> "mpg": 15, #> "cyl": 8, #> "disp": 301, #> "hp": 335, #> "drat": 3.54, #> "wt": 3.57, #> "qsec": 14.6, #> "vs": 0, #> "am": 1, #> "gear": 5, #> "carb": 8, #> "_row": "Maserati Bora" #> }, #> { #> "mpg": 21.4, #> "cyl": 4, #> "disp": 121, #> "hp": 109, #> "drat": 4.11, #> "wt": 2.78, #> "qsec": 18.6, #> "vs": 1, #> "am": 1, #> "gear": 4, #> "carb": 2, #> "_row": "Volvo 142E" #> } #> ]
# Parse it back fromJSON(jsoncars)
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 #> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 #> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 #> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 #> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 #> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 #> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 #> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 #> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 #> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 #> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 #> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 #> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 #> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 #> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 #> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 #> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 #> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 #> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 #> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 #> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 #> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 #> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# Parse escaped unicode fromJSON('{"city" : "Z\\u00FCrich"}')
#> $city #> [1] "Zürich" #>
# Decimal vs significant digits toJSON(pi, digits=3)
#> [3.142]
toJSON(pi, digits=I(3))
#> [3.14]
if (FALSE) { #retrieve data frame data1 <- fromJSON("https://api.github.com/users/hadley/orgs") names(data1) data1$login # Nested data frames: data2 <- fromJSON("https://api.github.com/users/hadley/repos") names(data2) names(data2$owner) data2$owner$login # Flatten the data into a regular non-nested dataframe names(flatten(data2)) # Flatten directly (more efficient): data3 <- fromJSON("https://api.github.com/users/hadley/repos", flatten = TRUE) identical(data3, flatten(data2)) }