R: lexical error: invalid char in json text

4.6k Views Asked by At

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)
}
1

There are 1 best solutions below

13
Phil On

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