1

This is the second time that I have faced this recently, so I wanted to reach out to see if there is a better way to parse dataframes returned from jsonlite when one of elements is an array stored as a column in the dataframe as a list.

I know that this part of the power with jsonlite, but I am not sure how to work with this nested structure. In the end, I suppose that I can write my own custom parsing, but given that I am almost there, I wanted to see how to work with this 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")

## get the data
x <- GET(URL)

## parse
api_response <- content(x, as="text")
api_response <- jsonlite::fromJSON(api_response, flatten=TRUE)

## get the data of interest
pbp <- api_response$data$game$plays$play
colnames(pbp)

And exploring what comes back:

> class(pbp$aoi)
[1] "list"
> class(pbp$desc)
[1] "character"
> class(pbp$xcoord)
[1] "integer"

From above, the column pbp$aoi is a list. Here are a 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 really care if I parse these lists in the same dataframe, but what do I have for options to parse out the data?

I would prefer to take the data out of out lists and parse them into a dataframe that can be "related" to the original record it came from.

Thanks in advance for your help.

3
  • 1
    perhaps playing around with purrr::unnest might get you what you need? Commented Feb 6, 2016 at 18:17
  • 1
    You were spot on. This get's at exactly what I needed to do. select(pbp, eventid, aoi) %>% unnest() %>% head. Thanks for the tip, I wasn't aware of this package/function. Commented Feb 6, 2016 at 18:50
  • 2
    I don't see unnest in purrr. Perhaps you meant pkg:tidyr? Commented Feb 7, 2016 at 3:18

1 Answer 1

2

From @hrbmstr above, I was able to get what I wanted using unnest.

select(pbp, eventid, aoi) %>% unnest() %>% head
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.