1

I would like to import a single object from a json file into a R data frame. Normally I use fromJSON() from the jsonlite package. However now I want to load this json into a data frame and then only the object that is called plays.

If I use:

library(jsonlite)
df <- fromJSON("http://live.nhl.com/GameData/20132014/2013020555/PlayByPlay.json")

It gives a data frame containing all the objects. Is there a way to only load the plays object in the data frame? Or should I just load the complete json and restructure this within R?

1 Answer 1

2

That does return a dataframe, although it 's kind of a mangled gemisch of list and dataframe. If you use a different package, it is just a list. Using str(df) (warning ...long output)

library(RJSONIO)    
str(df)
#------------
List of 1
 $ data:List of 2
  ..$ refreshInterval: num 0
  ..$ game           :List of 7
  .. ..$ awayteamid  : num 24
  .. ..$ awayteamname: chr "Anaheim Ducks"
  .. ..$ hometeamname: chr "Washington Capitals"
  .. ..$ plays       :List of 1
  .. .. ..$ play:List of 102
  .. .. .. ..$ :List of 28
  -----------Output truncated----------------

.... shows that the plays portions can be obtained with:

plays_out <- df$data$game$plays

I do not see that there is any advantage in trying to parse this yourself. Most of the "volume" of data is in the plays component.

When I use jsonlite::fromJSON I get a slightly different structure which is sufficiently different that I now I need to use a different call to get the plays items:

> str(df )
'data.frame':   1 obs. of  2 variables:
 $ refreshInterval:List of 1
  ..$ data: num 0
 $ game           :'data.frame':    1 obs. of  7 variables:
  ..$ awayteamid  :List of 1
  .. ..$ data: num 24
  ..$ awayteamname:List of 1
  .. ..$ data: chr "Anaheim Ducks"
  ..$ hometeamname:List of 1
  .. ..$ data: chr "Washington Capitals"
  ..$ plays       :'data.frame':    1 obs. of  1 variable:
  .. ..$ play:List of 1
  .. .. ..$ data:'data.frame':  102 obs. of  29 variables:
  .. .. .. ..$ aoi          :List of 102
  .. .. .. .. ..$ : num  8470612 8470621 8473933 8473972 8475151 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
  .. .. .. .. ..$ : num  8459442 8467332 8467400 8471476 8471699 ...
#------snipped output------------
> length(df$game$plays)
[1] 1
> length(df$game$plays$play)
[1] 1
> length(df$game$plays$play$data)
[1] 29

I think I prefer the result from RJSONIO::fromJSON, since it doesn't add the complexity of dataframe coercion.

Sign up to request clarification or add additional context in comments.

6 Comments

If I do plays_out <- df$data$game$plays after using the fromJSON command I get an empty value called plays_out. Am I missing something obvious here?
I'm not sure who is missing what. I used the fromJSON function in the 'RJSONIO' package which I loaded before I noticed that you mentioned "jsonlite"(without the quotes) in your text. That was why I added teh library call. I suppose it's possible that the structures we are looking at are different.
Thanks for the update. I switched to 'RJSONIO' package and this works.
And now struggling with getting this list into a data.frame. I already tried, from the plyr package, df <- ldply (plays_out, data.frame), but this gives errors. Is there a way to get this in a data.frame?
Which list? If you want to work with the first aoi value it would be as.data.frame(df$game$plays$play$data$aoi)
|

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.