0

How can make sure that the result of my getURL() call is properly formatted to be parsed using from JSON?


Details

If I take the string for api URL and paste that into Chrome, then copy and paste out the resulting JSON, RJSONIO::fromJSON() will parse it. However, if I pass the variable test, as in my code below, to fromJSON(), I get this error:

Error in fromJSON(content, handler, default.size, depth, allowComments, : invalid JSON input

In going through the differences between the two, I found some issues encoding escaped character sequences such as "\\\"\\\\\\\"" which I am able to search for and replace. However there are some other things, where for example, the broken JSON will show " " while the working JSON will show "\u00A0".

library(RJSONIO)
library(RCurl)

apiURL= #sorry I can't post the actual URL due to company security policies
test<-getURL(apiURL,userpwd="myusername:mypassword",httpauth=1L)
transcripts1 <- fromJSON(test)
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
  • Check out utils::URLencode – chinsoon12 Apr 06 '16 at 06:01
  • `"\\"\\\\""` is not a legitimate R character value. The first backslash escapes the second backslash, so the second double quote terminates the strings, making the rest of the string "not there" but also an error. – IRTFM Apr 06 '16 at 06:16
  • Can you post a demo url and paste the values of test. Some sample will be good to work with. – tushaR Apr 06 '16 at 06:22
  • @chinsoon12 : I just tried URLencode() on my JSON text, but it still failed when trying to parse it after – Misho Galbo Apr 06 '16 at 08:53
  • @42- : The plaintext is "doublequote backslash doublequote". "\\\"\\\\\\\"" is the expression that I need to find/replace using gsub. – Misho Galbo Apr 06 '16 at 08:54
  • @Tushar : As mentioned above, I cannot post the URL or sample JSON. However, the same URL generates working JSON text in chrome, but does not work with getURL. My question is how I can get the result of the API call to provide working JSON text in R, while I already know that that the URL provides functional JSON in chrome. – Misho Galbo Apr 06 '16 at 08:55
  • did you try test<-getURL(utils::URLencode(apiURL),userpwd="myusername:mypassword",httpauth=1L) and also try fromJSON(URLencode(test)) – chinsoon12 Apr 06 '16 at 09:14

0 Answers0