0

I'm trying to retrive data from an API, the request I get back is of type "application/json", however when I try to extract the content as text I get an empty string and, therefore, also the fromJSON command fails. The following code worked until yesterday, but now there must have been some changes in the API and I can't work my way around it.

Here's a snippet of how the raw req$content looks like:

[1] fe ff 00 7b 00 0d 00 0a 00 20 00 20 00 22 00 40 00 6f 00 64 00 61 00 74 00 61 00 2e 00 63 00 6f 00 6e 00 74 00 65 00 78
  [41] 00 74 00 22 00 3a 00 22 00 68 00 74 00 74 00 70 00 73 00 3a 00 2f 00 2f 00 73 00 65 00 72 00 76 00 69 00 63 00 65 00 73
  [81] 00 2d 00 61 00 70 00 69 00 2e 00 6c 00 65 00 78 00 69 00 73 00 6e 00 65 00 78 00 69 00 73 00 2e 00 63 00 6f 00 6d 00 2f
 [121] 00 76 00 31 00 2f 00 24 00 6d 00 65 00 74 00 61 00 64 00 61 00 74 00 61 00 23 00 4e 00 65 00 77 00 73 00 22 00 2c 00 22

This is the code that worked until yesterday and that I'm trying to use:

content = jsonlite::fromJSON(content(req, "text"), 
       simplifyVector = FALSE)
Error: parse error: premature EOF
                      (right here) ------^

I've also tried the following with no success:

dta <- req$content
dtan <- dta[!dta=='00']
dtanc <- rawToChar(dtan)
content = jsonlite::fromJSON(dtanc, simplifyVector = FALSE)
Error: lexical error: invalid char in json text.
                                       <fe><ff>{    "@odata.context":"
                     (right here) ------^
RNO
  • 1
  • It looks like your data is encoded in UTF-16 rather than UTF-8 which is weird because the format dictates the latter encoding. Maybe use `iconv` to convert the encoding before parsing. It would be easier to help if you provided some sort of [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) we could copy/paste into R. – MrFlick Aug 30 '19 at 14:25
  • 1
    Where you able to find a solution on this ? I have the same issue. – Philippe Nov 15 '19 at 09:25

0 Answers0