jdoubleyou
jdoubleyou

Reputation: 297

Cannot fetch data from URL NBA stats

I'm having an issue fetching JSON data from the stats.nba.com endpoints. For example, this url http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2016-17&SeasonType=Regular+Season&teamid=1610612761 works fine in my browser, but when I try to read it into R, I receive errors.

jsonlite::fromJSON, RJSONIO::fromJSON, RCurl::getURL, HTTR::get all hang forever until I kill them.

rjson::fromJSON gives me an error "unexpected character 'h'"

I'm not sure if there is something denying programmatic access, and how I would get around that.

R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit) 
Running under: macOS Sierra 10.12.3

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

`

Upvotes: 2

Views: 443

Answers (1)

Dave2e
Dave2e

Reputation: 24109

Try downloading the file first before trying to process it:

library(curl)
library(jsonlite)

curl_download("http://stats.nba.com/stats/teamgamelog?LeagueID=00&Season=2016-17&SeasonType=Regular+Season&teamid=1610612761", "nba.json")
jsonlist<-fromJSON( "nba.json")
df<-as.data.frame(jsonlist$resultSets$rowSet)
names(df)<-jsonlist$resultSets$headers[[1]]
parameters<-jsonlist$parameters

Upvotes: 3

Related Questions