将嵌套的json文件转换为R中的Dataframe

时间:2019-06-27 03:54:50

标签: r json dataframe

我有下面给出的json文件。我想将其转换为数据框。

json_file-> [{'a': "abc", "date": "20190506", "my_col":{"weather":10, "ice": 12}},
             {'a': "xyz", "date": "20190507", "my_col":{"summer":18, "hot": 14}}]

数据框应如下所示:

 a  date      mycol
abc 20190506 "weather":10, "ice": 12
xyz 20190507 "summer":18, "hot": 14

尝试:

json_file <- fromJSON(json_file)

json_file <- lapply(json_file, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

1 个答案:

答案 0 :(得分:0)

使用rjson fromJSON

进行检查
library(rjson)
l='[{"a": "abc", "date": "20190506", "my_col":{"weather":10, "ice": 12}},{"a": "xyz", "date": "20190507", "my_col":{"summer":18, "hot": 14}}]' 
l = fromJSON(l)
do.call(rbind, l)
     a     date       my_col
[1,] "abc" "20190506" List,2
[2,] "xyz" "20190507" List,2