0

I'm trying to pull data from a json file using a wildcard.

The data I'm trying to pull are the game stats for every player from this json file. They are under

http://statsapi-default-elb-prod-876255662.us-east-1.elb.amazonaws.com/api/v1/game/565711/boxscore

I've tested the jsonpath using this website

http://jsonpath.herokuapp.com/

If I enter this json path

teams..players..person.fullName

I get a list of all the players

"Shane Bieber",
"Jon Edwards",
"Max Moroff",
"Jake Bauers",
"Tyler Olson",
"Nick Wittgren",
"Carlos Carrasco",
"Leonys Martin",
"Neil Ramirez",
"Greg Allen",
"Kevin Plawecki",
"Brad Miller",
"Jordan Luplow".
etc...  

However when I put that code into r I get an error

Error: unexpected '*' in "  repos$teams$*"

I've tried the following codes

repos$teams$*$players$*$person$fullName
repos$teams$[*]$players$[*]$person$fullName
repos$teams$[[*]]$players$[[*]]$person$fullName

The end goal is to get all the games stats for each player in to a dataframe

Can someone please point me in the right direction? I'm new to json with R, thought I was doing pretty good, then this hit me between the eyes! ha!

Thanks!

Don Hessey
  • 67
  • 11

2 Answers2

0

Neither base R nor the tidy verse support jsonpath. The R language doesn't define * to mean wildcard at all. In R, you have to map over collections to extract values. In R, with the purrr package I might do something like this

# repos <- jsonlite::fromJSON("http://statsapi-default-elb-prod-876255662.us-east-1.elb.amazonaws.com/api/v1/game/565711/boxscore")

library(purrr)
map(repos$teams, pluck, "players") %>% 
  unlist(recursive = FALSE) %>% 
  map_chr(pluck, "person", "fullName")

First I map over the teams to extract both home and away, then to each of those groups I use the pluck() function to get the players. Then I use unlist() to combine the lists of players from home/away. Then I map over all the players again using pluck() to extract the person$fullName from each one. The map_chr will ensure that a character vector is returned. Pluck will name that vector, if you want to drop the names you can can add %>% unname() at the end.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Well, that at least makes me feel a little bit better that R can't do it. ha! Your code makes perfect sense, and I see exactly what your saying. How would I grab the whole stats list? Would I have to do it one by one and bind the columns or is there a way to grab that section along with the player id or name – Don Hessey Apr 15 '19 at 23:03
0

You might try using the rjsonpath package. It's not on CRAN yet, but is useful here.

Once installed, you could do:

json_path(repos,  "$teams..players..person.fullName")

or maybe just

json_path(repos,  "$..fullName")
Neal Fultz
  • 9,282
  • 1
  • 39
  • 60