Convert JSON To R
fromJSON.RdConvert a JSON object into an R object.
Arguments
- json_str
a JSON object to convert
- file
the name of a file to read the json_str from; this can also be a URL. Only one of json_str or file must be supplied.
- method
use the
Cimplementation, or the older slower (and one day to be depricated)Rimplementation- unexpected.escape
changed handling of unexpected escaped characters. Handling value should be one of "error", "skip", or "keep"; on unexpected characters issue an
error,skipthe character, orkeepthe character- simplify
If TRUE, attempt to convert json-encoded lists into vectors where appropriate. If FALSE, all json-encoded lists will be wrapped in a list even if they are all of the same data type.
Examples
fromJSON('[1,2,3]', simplify=TRUE)
#> [1] 1 2 3
# returns c(1,2,3)
fromJSON('[1,2,3]', simplify=FALSE)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
#>
# returns list(1,2,3)
#As a result, this will output "1"
toJSON(fromJSON('[1]', simplify=TRUE))
#> [1] "1"
#Compared with this which will output "[1]" as expected
toJSON(fromJSON('[1]', simplify=FALSE))
#> [1] "[1]"
#R vs C execution time
x <- toJSON( iris )
system.time( y <- fromJSON(x) )
#> user system elapsed
#> 0.001 0.000 0.001
system.time( y2 <- fromJSON(x,method = "R") )
#> user system elapsed
#> 0.02 0.00 0.02