I want to know if there is any way we can convert json strings to a list in a vectorized fashion, in other words, I want to know if we can use a list of json to convert into a list of lists in R.
There are many options to convert from Json to list. however, all of them only accept one json string at time.
I have following list of json format:
x =
c("{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}",
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]})
Now I want to convert them to list. Right now I use the foreach
package to convert them into list. The following is code is what I use:
library(jsonlite)
library(foreach)
library(doParallel)
registerDoParallel(cores = 2)
z <- foreach(i = 1:length(x), .combine = 'rbind') %dopar% {
fromJSON(x[1])}
However, I want to do something like following:
z <- Vectroize_fromJSON(x)
Any ideas?
Note: The reason using foreach
is that I have found it to be faster than a normal for
loop. However, when the list of of json is large, then it becomes impractical.