0

I am a novice R user and definitely new with the xml format, so forgive me if there is an obvious answer to this question.

I have a dataset with 300 pairs of xy coordinates. I use these points to make a URL request in R and the output is returned in JSON format (I think). I am trying to create a data frame with specific objects from JSON output, and am running into some issues.

I've saved the content into a data frame and use the "fromJSON()" command to convert it to something usable in R so I can reference certain parts of the content using an array. However, for a select few of these observations, I get the following error:

"Error in parseJSON(txt) : lexical error: inside a string, '\' occurs before a character which it may not."

Someone I know suggested this error may have occurred because the parseJSON function is trying to interpret the ‘\’ as an escape character. I've since tried to replace all backslashes with double backslashes without much luck. I did notice that while using the gsub command, a single backslash was not interpretable in R, so I replaced with a double backslash, but also with no luck. I've also tried skipping over the backslashes using the scan function. Here is the code that I've tried:

res <- gsub('\\','\\\\',res)

res = scan(res,allowEscapes=F)

When I run gsub above, I get an error involving trailing backslashes. When I run scan, the file is uninterpretable by R. I also still want to be able to reference objects in R, so I'm not sure if using something other than '\' would be appropriate.

Could someone help me out? Below is my full code without any modifications to the backslashes (this is just for one set of points for which this error popped up):

homelat = 33.74943;
homelong = -84.49547;
care1final_lat = 33.80157;
care1final_long = -84.31166;
time = '10:00am'

url <- URLencode(paste("http://opentrip.atlantaregion.com/otp-rest-servlet/ws/plan?&fromPlace=", homelat, ",",homelong,"&toPlace=", care1final_lat, ",",care1final_long,"&time=",time,"&date=03-21-2014&mode=TRANSIT,WALK&maxWalkDistance=3218.688&arriveBy=false&showIntermediateStops=false&itinIndex=0",sep = ""))

res = getURL(url)

fn <- paste("otp",i,".json",sep="")
write(res, file = fn)

data <- fromJSON(res)

Thank you in advance!

  • I'm confused. You say you're working with XML but then you're using JSON commands. XML and JSON are very different things. If you are working with JSON files, you can validate then with [jsonlint](http://jsonlint.com/) to make sure they are proper JSON files. If you're working with XML, check out the `XML` package instead. It would help if you included a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) here that we could run to re-create the same error as you. – MrFlick Oct 08 '14 at 20:16
  • Post the output of `dput(res)`. – IRTFM Oct 08 '14 at 20:20
  • Yes, I'm very sorry -- I've posted more details above...is this helpful at all? Thank you so much! Let me know if you need more information. – emorystudent Oct 08 '14 at 20:50
  • I've had a look at the data returned from that URL. It is formatted as XML so you want to steer clear of Json commands as that's a very different encoding. As has been mentioend there is an XML package which should help. – Nathan Hatch Oct 11 '14 at 20:04

0 Answers0