如何使用tidyjson包将JSON文件转换为数据框?

时间:2018-09-25 10:55:28

标签: r json

我使用tidyjson包将json文件转换为数据帧,如下所示:

 json%>%as.tbl_json%>% spread_values(id = jstring("_id"))%>%
                 enter_object("courses")%>%
                 spread_values(course_id = jstring( "course_id"),
                              created_at = jstring("created_at"),
                               update_at = jstring("updated_at"),
                               percentage = jnumber("percentage"),
                               category = jstring( "category"),
                               course_length = jnumber( "course_length"),
                               learn_length = jnumber( "learn_length"))

但是我得到了意外的结果。

         document.id id        course_id created_at update_at percentage category
   <int> <chr>     <chr>     <chr>      <chr>          <dbl> <chr>   
       1 55aa41af~ NA        NA         NA                NA NA       with 2 more variables: course_length <dbl>, learn_length <dbl>

您能帮我找到我错了吗? 这是我要转换的json文件:

json <- '{"_id": "55aa41af736773421a000000",   "courses": [{"course_id": "55b1c17152696418a000005b",
"created_at": "2018-08-13 09:56:00.099000",
"updated_at": "2015-08-14 13:49:00.545000",
"percentage": 13.938669852648347,
"category": "Language",
"course_length": 2511,
"learn_length": 350.0}]}'

1 个答案:

答案 0 :(得分:0)

像这样吗?

require(jsonlite);require(dplyr)
te <- fromJSON(json) %>% as.data.frame()

欢呼