-1

How do you converting an uneven json file into tidy data? Here I have an uneven json file, and I'd like to change into tidy data.

library(tidyverse)
library(jsonlite)

read_json("https://api.osf.io/v2/search/?q=*&page=100&format=jsonapi") %>% 
  as_tibble()
#> Error: Tibble columns must have compatible sizes.
#> * Size 5: Column `links`.
#> * Size 6: Column `search_fields`.
#> * Size 10: Column `data`.
#> ℹ Only values of size one are recycled.
John-Henry
  • 1,556
  • 8
  • 20

1 Answers1

1

It is hard to come up with a concrete answer to a question so open. The data is also heavily nested, and it is not clear, how you want to unnest this properly. A place to start however is to unnest_wider each of the elements in your list, and then try to figure where you want to go from there

library(tidyverse)
library(jsonlite)

data <- read_json("https://api.osf.io/v2/search/?q=*&page=100&format=jsonapi")
unnest_wider(d = tibble::tibble(data[[1]]), c(d))
# The second element seems to be doubly nested
unnest_wider(unnest_wider(d = tibble::tibble(data[[2]]), c(d)), c(related))
unnest_wider(d = tibble::tibble(data[[3]]), c(d))
unnest_wider(d = tibble::tibble(data[[4]]), c(d))

From there you can merge, unnest certain columns further and so forth, but without more information it is hard to come with anything more concrete.

Oliver
  • 8,169
  • 3
  • 15
  • 37
  • Thanks for the response! However I'm getting `Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "list"` error – John-Henry Jul 31 '20 at 22:54
  • Right, i forgot to wrap `data[[i]]` in `tibble::tibble(..)`` I'll update it quickly. – Oliver Jul 31 '20 at 23:08