5

I've been trying to import a JSON file into R for a while using both the rjson and RJSONIO package, but I can't get it to work. One of the variations of code I used:

json_file <- "http://toolserver.org/~emw/index.php?c=rawdata&m=get_traffic_data&p1=USA&project1=en&from=12/10/2007&to=4/1/2011"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

This results in an error message:

Error in fromJSON(paste(readLines(json_file), collapse = "")) : 
unexpected character '<'

I think the problem is in the first line of code, because json_file contains the source of the website, not the actual content. I've tried getURL() and getURLContent(), but without any success. Any help would be much appreciated!

Edit: as pointed out by Martin Morgan, the problem seems to be with the URL, not the code!

4
  • 2
    When I visit the url in my browser and view the page source, it is pure HTML, with no JSON. So Commented Sep 25, 2011 at 14:31
  • Thanks for checking! You're right, but what is displayed on the page is JSON. Do you know of a way to grab that? I've tried some functions in the RCurl package, but without any luck so far. Commented Sep 25, 2011 at 16:45
  • 1
    Previously, typing the URL into the browser would generate a blank page and fromJSON(json_file) would error as you indicate. Just now, the browser displays JSON, and library(RJSONIO); fromJSON(json_file) works as expected. It seems like the service is not behaving consistently, either because of an issue on 'their' end or because it is meant to be invoked in a more subtle way than just pointing at a URL Commented Sep 25, 2011 at 17:06
  • Oh wow, it does work with this URL! I'll contact the owner of the tool. Thanks so much for following up! Commented Sep 25, 2011 at 18:40

1 Answer 1

4
library(rjson)
fromJSON(readLines('http://toolserver.org/~emw/index.php?c=rawdata&m=get_traffic_data&p1=USA&project1=en&from=12/10/2007&to=4/1/2011')[1])

works for me, with a warning

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.