#tidyjson
Explore tagged Tumblr posts
Text
How to flatten json in R with tidyjson
library(dplyr) library(tidyjson) read_json("file.json") %>% # change it select(-document.id) %>% enter_object(nestedElement1, nestedElement2, arrayElement) %>% # change it gather_array("document.id") %>% json_structure %>% filter(type != "object" & type != "array" & type != "null") %>% mutate(path = sapply(seq, function(i) paste(i, collapse = ".")), value = ..JSON) %>% select(matches("document.id|path|value")) %>% as_tibble() %>% pivot_wider(names_from = path, values_from = value) %>% mutate(across(everything(), ~unlist(replace_na(., NA)))) %>% identity -> result
0 notes