Given this example data frame I can easily convert the nested json files into a flattened list, which I can then in subsequent steps convert into a dataframe with one column per json entry:
sample.df.a <- data.frame(json_col = c('[{"foo_a":"_","foo_c":2}]',
'[{"foo_a":"_","foo_b":"_","foo_c":2,"nested_col":{"foo_d":"_","foo_e":3}}]'))
sample.df.a.list <- apply(sample.df.a, 1, jsonlite::fromJSON, flatten = T)
However my actual data that I need to work with has the following format:
sample.df.b <- as.data.frame(apply(sample.df.a, 1, toJSON))
(this is how the data has come to me and can't be changed, and isn't a result of a toJSON
conversion as in this engineered example. With my actual data when I try to collapse the nested json into lists (the desired output, as is given with sample.df.a.list
) it instead returns a character which I cannot then subsequently convert into a dataframe, like so:
sample.df.b.list <- apply(sample.df.b, 1, jsonlite::fromJSON, flatten = T)
Does anyone know how can I create the same sort of collapsed list as sample.df.a.list
from sample.df.b
?
Thanks in advance!
FYI: subsequent code to vonvert the lists into a dataframe:
library(dpylr)
list.a.as.df <- bind_rows(lapply(sample.df.a.list,data.frame))