Преобразование вложенного JSON во фрейм данных - PullRequest
0 голосов
/ 02 декабря 2018

Я пытаюсь преобразовать сильно вложенный json в правильный фрейм данных (по стандартам аккуратности).MWE json копируется в конце вопроса, так как я хотел убедиться, что он захватил каждый элемент 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"
            }
        ]
    }
}

1 Ответ

0 голосов
/ 02 декабря 2018

Использование flatten = TRUE кажется ключевым здесь:

dat <- jsonlite::fromJSON('data_toy.json', flatten = TRUE)[[1]]
dat %>% bind_rows() %>% mutate(department = rep(names(dat), map_dbl(dat, nrow)))
...