0

I am converting from python to R and this is my first attempt at Parsing Json. I have tried jsonlite, RJSONIO, and rjson. All of them stop at the getlabskaters <- fromJSON(getlabskatersRaw). To explain this script a little bit, because I clearly need some help on how to properly parse json It starts buy going to one website that returns some teams that I than use in the for loop to return all the players with that list of teams. Whats weird is the error does not occur when I run the above fromJSON it only happens when I put it into the for loop. I have checked the paste0 and the links by print(getlabskatersRaw). All of them appear fine to browse to. Some help with the error would be awesome and if your bored and have any advice on better parsing tactics I am all ears.

library(magrittr)
library(readr)
library(tidyr)
library(dplyr)
library(lubridate)
library(jsonlite)
library(httr)
library(stringi)
library(tidyverse)
library(httr)

# GET Todays date
today <- Sys.Date()

#Json return from URL
getlabteamsRaw<-paste0('https://www.fantasylabs.com/api/lines/4/', as.character(today),'/startinggoalies')

# Turn Value into List
getlabteams <- fromJSON(getlabteamsRaw)

# Parse home team
team<-getlabteams$GoalieMatchups$Properties$HomeTeam

# Convert 
Home_Fullname<-as.data.frame(team)
team<-getlabteams$GoalieMatchups$Properties$VisitorTeam
Away_Fullname<-as.data.frame(team)
LabTeams <- full_join(Home_Fullname, Away_Fullname, by = c("team"))

# PLAYER INDIVIDUAL NULL DF's
lab_skaters_df <- NULL

for(labtm in LabTeams){
  getlabskatersRaw<-paste0('https://www.fantasylabs.com/api/lines/4/', as.character(labtm),'/', as.character(today))

  ### THE ERROR IS HERE########
  getlabskaters <- fromJSON(getlabskatersRaw)

  # Parse Player name
  new<-getlabskaters$PlayerLines$Properties$FullName

  # Convert Value to Dataframe
  Fullname<-as.data.frame(new)

  #APPEND FEATURE 
  lab_skaters_df <- rbind(lab_skaters_df, Fullname)
}
Mike.J
  • 117
  • 1
  • 10

1 Answers1

0

I recognize this is a non-answer as I'm just dumping in code, and not explaining the issue, but this will work for you. I also cleaned up your code (you're notably loading up a bunch of packages you're not actually using, or are already loaded via library(tidyverse)), and I'm using the map_df function from the purrr package to loop through the teams instead of a for loop.

library(jsonlite)
library(tidyverse)

# GET Todays date
today <- Sys.Date()

#Json return from URL
getlabteamsRaw <- paste0('https://www.fantasylabs.com/api/lines/4/', today, '/startinggoalies')

# Turn Value into List
getlabteams <- fromJSON(getlabteamsRaw)

teams <- c(getlabteams$GoalieMatchups$Properties$HomeTeam, getlabteams$GoalieMatchups$Properties$VisitorTeam)

myfun <- function(x) {
  getlabskatersRaw <- paste0('https://www.fantasylabs.com/api/lines/4/', x,'/', today)
  getlabskaters <- fromJSON(getlabskatersRaw)
  select(getlabskaters$PlayerLines$Properties, FullName, Position, Position, Line, Team, OppTeam, Salary_DK, Salary_FD, ActualPoints_DK, ActualPoints_FD, ImpPts_DK, ImpPts_FD)
}

myfinal_df <- map_df(teams, myfun)

# Wait a few seconds
myfinal_df
               FullName Position Line               Team             OppTeam Salary_DK Salary_FD
1       Phillip Danault       1C   1F Montreal Canadiens  Chicago Blackhawks      5400      5500
2           Carey Price       1G   1G Montreal Canadiens  Chicago Blackhawks      7900      8400
3           Ben Chiarot      1LD   1D Montreal Canadiens  Chicago Blackhawks      4400      4000
4           Tomas Tatar      1LW   1F Montreal Canadiens  Chicago Blackhawks      5800      6400
5            Shea Weber      1RD   1D Montreal Canadiens  Chicago Blackhawks      6400      6200
Phil
  • 7,287
  • 3
  • 36
  • 66
  • Thanks Phil, your right it does work and cleans up my code a lot. There are other values I would like to grab along with the "player" value. Do I just copy enframe line and repeat or add to the value = part in enframe? Also, How do I turn that tibble into dataframe to access? – Mike.J Jan 15 '20 at 05:16
  • A tibble is a data frame, but feel free to use `as.data.frame()` if you're not comfortable with it. For your first question, probably the easiest thing to do is to repeat the process of creating a function for each set of values you want, and then repeat the usage of `map_df` for each of those functions, and store each of those into their own data frames/tibbles, and then combine them with `left_join()`. – Phil Jan 15 '20 at 05:20
  • Ok, thanks for the knowledge and quick response! Haven't worked with functions yet in R so I should definitely start now! What is the name of the tibble? would I just do `new_df<-as.data.fram(nameoftibble)`? – Mike.J Jan 15 '20 at 05:22
  • If you have an example of other information that you want to pass through, it could probably all be done in the same custom function call, which would be far more efficient. Without knowing exactly what you want, it's tough to demonstrate how. – Phil Jan 15 '20 at 05:25
  • Position, Line, Team, OppTeam, Salary_DK, Salary_FD ,ActualPoints_DK ,ActualPoints, ImpPts_DK, ImpPts_FD. These were the others ones I needed – Mike.J Jan 15 '20 at 05:29
  • Not sure if you saw the line above. If you could help show me how to gather the others and convert to a DF that would be awesome. I am having some troubles as I am not familiar with the language in your script. – Mike.J Jan 15 '20 at 16:15
  • I edited the function to return those variables, and it will no longer return a tibble. To answer your other question, yes that would do it to set a tibble to a data frame. Note that I don't understand why you'd want to do this though, as tibbles are an improvement on data frames. To me, this is making something suboptimal. – Phil Jan 15 '20 at 17:43
  • Being new I do not understand how to work with Tibble.. I am trying to merge two dataframes and I know only how to do that. – Mike.J Jan 15 '20 at 17:47
  • You can learn more here: https://blog.rstudio.com/2016/03/24/tibble-1-0-0/ You can merge 2 tibbles in the exact same way you can merge 2 data frames, since they're essentially the same thing. – Phil Jan 15 '20 at 17:50
  • I will do so. Temporarily could you write a quick line to convert to dataframe. Im not sure what the Tibble is called to do that. – Mike.J Jan 15 '20 at 17:52
  • Sorry for the confusion but I am use to dataframes showing up in the Data area and being viewable. What is the name of the tibble so I can say new_df<-as.data.frame(tibble) – Mike.J Jan 15 '20 at 18:10
  • I edited the code so that the output is now saved in a data frame object `myfinal_df`. Running `myfinal_df` in the console will display the data frame in your console. – Phil Jan 15 '20 at 18:13
  • Thank you very much Phil. I will do my research on tibble as I have many many datatframes that dont need to be dataframes. – Mike.J Jan 15 '20 at 18:22