The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Type Mapping

library(RJSONIO)

JSON and R have similar but not identical type systems. This article summarizes the common mappings used by fromJSON() and toJSON().

JSON to R

#>         JSON                                            R
#> 1     object        named list or simplified named vector
#> 2      array             list or simplified vector/matrix
#> 3     string                                    character
#> 4     number                                      numeric
#> 5 true/false                                      logical
#> 6       null NULL or the value supplied through nullValue
str(fromJSON('{"a": 1, "b": 2}', simplify = FALSE))
#> List of 2
#>  $ a: num 1
#>  $ b: num 2
str(fromJSON("[1, 2, 3]"))
#>  num [1:3] 1 2 3
str(fromJSON('["a", "b", "c"]'))
#>  chr [1:3] "a" "b" "c"
str(fromJSON("[true, false]"))
#>  logi [1:2] TRUE FALSE
str(fromJSON("[1, null, 3]", nullValue = NA, simplify = TRUE))
#>  num [1:3] 1 NA 3

R to JSON

#>                     R                              JSON
#> 1          named list                            object
#> 2        unnamed list                             array
#> 3       atomic vector                             array
#> 4 named atomic vector                            object
#> 5              matrix                      nested array
#> 6          data.frame object by column, or array by row
#> 7                  NA                              null
#> 8                NULL                              null
toJSON(list(a = 1, b = TRUE))
#> [1] "{\n \"a\":        1,\n\"b\": true \n}"
toJSON(list(1, TRUE, "x"))
#> [1] "[\n        1,\ntrue,\n\"x\" \n]"
toJSON(c(1, 2, 3))
#> [1] "[        1,        2,        3 ]"
toJSON(c(a = 1, b = 2))
#> [1] "{\n \"a\":        1,\n\"b\":        2 \n}"
toJSON(matrix(1:4, nrow = 2))
#> [1] "[ [ 1, 3 ],\n[ 2, 4 ] ]"
toJSON(data.frame(a = 1:2, b = c("x", "y")))
#> [1] "{\n \"a\": [ 1, 2 ],\n\"b\": [ \"x\", \"y\" ] \n}"
toJSON(NA)
#> [1] "[ null ]"
toJSON(NULL)
#> [1] "[ null ] "

Empty containers

Empty containers can carry different meaning depending on names.

toJSON(list())
#> [1] "[]"
toJSON(emptyNamedList)
#> [1] "{}"

fromJSON("[]")
#> list()
fromJSON("{}")
#> named list()

Simplification constants

The exported constants can be combined to control simplification.

Strict
#> [1] 14
StrictNumeric
#> [1] 4
StrictCharacter
#> [1] 8
StrictLogical
#> [1] 2

fromJSON('[1, true]', simplify = Strict)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] TRUE
fromJSON('[1, true]', simplify = TRUE)
#> [1] 1 1

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.