r - Nested List Parsing with jsonlite -
this second time have faced recently, wanted reach out see if there better way parse dataframes returned jsonlite when 1 of elements array stored column in dataframe list.
i know part of power jsonlite, not sure how work nested structure. in end, suppose can write own custom parsing, given there, wanted see how work data.
for example:
## options options(stringsasfactors=f) ## packages library(httr) library(jsonlite) ## setup gameid="2015020759" season = '20152016' base = "http://live.nhl.com/gamedata/" url = paste0(base, season, "/", gameid, "/playbyplay.json") ## data x <- get(url) ## parse api_response <- content(x, as="text") api_response <- jsonlite::fromjson(api_response, flatten=true) ## data of interest pbp <- api_response$data$game$plays$play colnames(pbp) and exploring comes back:
> class(pbp$aoi) [1] "list" > class(pbp$desc) [1] "character" > class(pbp$xcoord) [1] "integer" from above, column pbp$aoi list. here few entries:
> head(pbp$aoi) [[1]] [1] 8465009 8470638 8471695 8473419 8475792 8475902 [[2]] [1] 8470626 8471276 8471695 8476525 8476792 8477956 [[3]] [1] 8469619 8471695 8473492 8474625 8475727 8476525 [[4]] [1] 8469619 8471695 8473492 8474625 8475727 8476525 [[5]] [1] 8469619 8471695 8473492 8474625 8475727 8476525 [[6]] [1] 8469619 8471695 8473492 8474625 8475727 8475902 i don't care if parse these lists in same dataframe, have options parse out data?
i prefer take data out of out lists , parse them dataframe can "related" original record came from.
thanks in advance help.
from @hrbmstr above, able wanted using unnest.
select(pbp, eventid, aoi) %>% unnest() %>% head
Comments
Post a Comment