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 3R 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 ] "