简体   繁体   中英

convert json to list in a vectorized way in R

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.

Is this something close to what you want?

vectorize_fromJSON <- Vectorize(fromJSON)
z <- vectorize_fromJSON(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM