我正试图将一个嵌套严重的json转换为一个合适的数据帧(按照整洁的标准来说是合适的(。json的MWE在问题的末尾被复制,因为我想确保它捕获json的每个元素。
我试过:
library(jsonlite)
library(tidyverse)
dat <- jsonlite::fromJSON('data_toy.json') %>% pluck(1) %>% imap_dfr(~mutate(.x, department = .y))
但这会返回:
Error: Columns `time_spent`, `school_breakdown`, `reason_for_taking_course`, `student_years`, `interest_before` must be 1d atomic vectors or lists
我也试过:
dat <- jsonlite::fromJSON('data_toy.json', simplifyVector = FALSE,
simplifyDataFrame = FALSE, flatten=FALSE)
dat.df <- map_df(dat, ~{
flatten_df(.x[[1]]) %>%
dplyr::mutate(department = names(.x)[1])
})
但这会返回:
Error in bind_rows_(x, .id) : Argument 3 must be length 1, not 0
如何将其转换为数据帧?
数据文件(Data_toy.json(:
{
"department": {
"BME": [
{
"course_name": "BMD_ENG_250-0_20: Thermodynamics",
"instructor": "Neha Kamat",
"time_spent": {},
"school_breakdown": {
"Education & SP": 0,
"Communication": 0,
"Graduate School": 0,
"KGSM": 0
},
"reason_for_taking_course": {
"Distribution requirement": 0,
"Major/Minor requirement": 53
},
"student_years": {
"Freshman": 5,
"Sophomore": 37
},
"interest_before": {
"1-Not interested at all": 1,
"2": 5
},
"comments": [
"is amazing and you will love her!",
"Prof. is so nice"
],
"instructor_gender": "F"
},
{
"course_name": "BMD_ENG_250-0_20: Thermodynamics",
"instructor": "Neha Kamat",
"time_spent": {},
"school_breakdown": {
"Education & SP": 0,
"Communication": 0,
"Graduate School": 0,
"KGSM": 0
},
"reason_for_taking_course": {
"Distribution requirement": 0,
"Major/Minor requirement": 53
},
"student_years": {
"Freshman": 5,
"Sophomore": 37
},
"interest_before": {
"1-Not interested at all": 1,
"2": 5
},
"comments": [
"is amazing and you will love her!",
"Prof. is so nice"
],
"instructor_gender": "F"
}
],
"LING": [
{
"course_name": "BMD_ENG_250-0_20: Thermodynamics",
"instructor": "Neha Kamat",
"time_spent": {},
"school_breakdown": {
"Education & SP": 0,
"Communication": 0,
"Graduate School": 0,
"KGSM": 0
},
"reason_for_taking_course": {
"Distribution requirement": 0,
"Major/Minor requirement": 53
},
"student_years": {
"Freshman": 5,
"Sophomore": 37
},
"interest_before": {
"1-Not interested at all": 1,
"2": 5
},
"comments": [
"is amazing and you will love her!",
"Prof. is so nice"
],
"instructor_gender": "F"
},
{
"course_name": "BMD_ENG_250-0_20: Thermodynamics",
"instructor": "Neha Kamat",
"time_spent": {},
"school_breakdown": {
"Education & SP": 0,
"Communication": 0,
"Graduate School": 0,
"KGSM": 0
},
"reason_for_taking_course": {
"Distribution requirement": 0,
"Major/Minor requirement": 53
},
"student_years": {
"Freshman": 5,
"Sophomore": 37
},
"interest_before": {
"1-Not interested at all": 1,
"2": 5
},
"comments": [
"is amazing and you will love her!",
"Prof. is so nice"
],
"instructor_gender": "F"
}
]
}
}
使用flatten = TRUE
似乎是这里的关键:
dat <- jsonlite::fromJSON('data_toy.json', flatten = TRUE)[[1]]
dat %>% bind_rows() %>% mutate(department = rep(names(dat), map_dbl(dat, nrow)))