reading a json file in R: lexical error: invalid char in json text reading a json file in R: lexical error: invalid char in json text r r

reading a json file in R: lexical error: invalid char in json text


This happened to me reading in a json from a file. The code worked one day, and then the next day I got this error. I was eventually able to circumvent the error although I do not understand why my solution works. First, I found a github post that suggested adding the readLines() function. Eg.

r_object <- fromJSON(readLines("file.json"))

When I did this the json loaded correctly but I got the following warning:

Warning message:In readLines("file.json") : incomplete final line found on 'file.json'.

Then, for no particular reason, I tried adding an additional line to the bottom of the JSON. Just a blank line after the last curly bracket. And that fixed it. No idea why. If anyone knows why this worked, please drop a comment.


I can't replicate error neither.

class(results_data_json)[1] "list"

My sessioninfo:

R version 3.3.2 (2016-10-31)Platform: x86_64-w64-mingw32/x64 (64-bit)Running under: Windows 7 x64 (build 7600)locale:[1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252    LC_MONETARY=Spanish_Colombia.1252[4] LC_NUMERIC=C                      LC_TIME=Spanish_Colombia.1252    attached base packages:[1] stats     graphics  grDevices utils     datasets  methods   base     other attached packages:[1] curl_2.4     jsonlite_1.1loaded via a namespace (and not attached):[1] tools_3.3.2


On my mac, it was caused by iCloud. The json file had been stored in the cloud, and R couldn't find it. Downloading the file fixed it. This may or may not have been what happened in your case.

Apologies that I'm nearly five years too late.