I am trying to save some county climate data onto a .csv and have saved some responses that I accessed, using the GET() function for API calls from the httr package. However, when I try to convert the content of the response into a dataframe using fromJSON(), I am constantly hit by a lexical error that there is an "invalid char in json text."
I have been looking through many similar issues on Stack Overflow and thought it might be somehow related to the content type being HTML. The validate() function from jsonlite also verifies that the output of rawToChar() on the content of my response is not in the JSON format. However I am not familiar enough with the httr or jsonlite packages to understand the exact issue I am encountering or how to resolve it.
Is there an issue with the call, the response, or something else? Any suggestions would be helpful.
Here is the relevant excerpt of the exact code I have been using (modified for a single week instead of a year):
test_county <- GET("https://nass.agroclimate.org/ws/data/daily.php?county=USAL01001&sdate=20220101&edate=20220108&csv")
df <- fromJSON(rawToChar(test_county$content), flatten = TRUE)
I was expecting this code to output a dataframe from the content of the response to my API call, but instead I received the subsequent error message:
Error: lexical error: invalid char in json text.
date,nass_week,nass_week_year,c
(right here) ------^