jsonrrjsonio

How do I pull .js or .json data from a specific URL into an R data frame


I have been trying to pull data from this specific URL: http://leafletjs.com/examples/us-states.js using RJSONIO. I went to that URL and saved the data by pressing CTRL+A to select all of the data there and then pasted it into notepad++, saving it as test.json. Here is what I have tried in R from that point:

library(RJSONIO)
json_file <- dir("test.json")
jsonIntoR <- fromJSON(readLines(json_file)[1])

And I get the following error:

Error in fromJSON(readLines(json_file)[1]) : 
    error in evaluating the argument 'content' in selecting a method for function 'fromJSON': Error in file(con, "r") : invalid 'description' argument

I'd like to convert the data at this URL into a dataframe, but have not been able to overcome this error. I have tried using the solution at this link: Import JSON file from URL into R but it does not work for me. Thank you for your help.


Solution

  • Don't use readLines(...).

    library(rjson)
    library(httr)
    url <- "http://leafletjs.com/examples/us-states.js"
    text <- content(GET(url),type="text")
    text <- sub("var statesData = ","",text)
    text <- sub(";$","",text)
    json <- fromJSON(text)
    

    Your file is actually javascript, so to make it interpretable as json you need to strip off the leadling var statesData = and the trailing ;.