首页 > 解决方案 > 将嵌套的json文件转换为R中的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)
})

标签: rjsondataframe

解决方案


检查与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

推荐阅读