Home > Software engineering >  expanding a named list inside a dataframe
expanding a named list inside a dataframe

Time:10-14

I have a number of JSON files in the following format

{ "year": 2019,
  "numberofhomes": 480,
  "meetingdate": "2019-02-09",
  "votes": 
    [
        { "date": "2019-01-23", "votes": 39 },
        { "date": "2019-02-01", "votes": 124 },
        { "date": "2019-02-09", "votes": 164 }
    ]
}

While reading this in with jsonlite::read_json, the resulting column for votes is a named list.

jsonlite::read_json("sources/votes-2019.json", simplifyDataFrame = FALSE) %>% 
   as_tibble()

# A tibble: 3 x 4
   year numberofhomes meetingdate votes
  <int>         <int> <chr>       <list>
1  2019           480 2019-02-09  <named list [2]>
2  2019           480 2019-02-09  <named list [2]>
3  2019           480 2019-02-09  <named list [2]>

Or the alternative

jsonlite::read_json("sources/votes-2019.json", simplifyDataFrame = TRUE) %>% 
   as_tibble()

# A tibble: 3 x 4
   year numberofhomes meetingdate votes$date $votes
  <int>         <int> <chr>       <chr>       <int>
1  2019           480 2019-02-09  2019-01-23     39
2  2019           480 2019-02-09  2019-02-01    124
3  2019           480 2019-02-09  2019-02-09    164

How can I transform the last column(s) into a normal dataframe column? Alternatively, is there a better way to read in JSON files with nested arrays?

CodePudding user response:

You can use unnest_wider:

library(tibble)
jsonlite::read_json("sources/votes-2019.json", simplifyDataFrame = FALSE) %>%
  as_tibble() %>%
  unnest_wider(votes)
  •  Tags:  
  • r
  • Related