Convert R To JSON
toJSON.RdConvert an R object into a corresponding JSON object.
***Lists with unnamed components are not currently supported***
Examples
x <- list( alpha = 1:5, beta = "Bravo",
gamma = list(a=1:3, b=NULL),
delta = c(TRUE, FALSE) )
json <- toJSON( x )
fromJSON( json )
#> $alpha
#> [1] 1 2 3 4 5
#>
#> $beta
#> [1] "Bravo"
#>
#> $gamma
#> $gamma$a
#> [1] 1 2 3
#>
#> $gamma$b
#> NULL
#>
#>
#> $delta
#> [1] TRUE FALSE
#>
#named vectors are treated as JSON objects (lists)
toJSON(islands[1:4])
#> [1] "{\"Africa\":11506,\"Antarctica\":5500,\"Asia\":16988,\"Australia\":2968}"
#data.frames must be converted into a list before converting into JSON
plot(cars, pch=2)
json_cars <- toJSON(as.list(cars))
points( data.frame( fromJSON( json_cars ) ), col="red", pch=3 )
#special R types are encoded as strings
testString <- c(1,2,3,4,NA,NaN,Inf,8,9);
toJSON(testString);
#> [1] "[1,2,3,4,\"NA\",\"NaN\",\"Inf\",8,9]"