I am trying to use the code shown below to extract data from a json file. However, the following error is returned:
Error: lexical error: invalid bytes in UTF8
The problem is that the URL returns data in a latin1
encoding, and your system is defaulting to reading it as UTF-8. You can get it correctly using
library(jsonlite)
library(RCurl)
URL <- "https://www.energy-charts.de/power_unit/month_lignite_unit_2017_12.json"
data <- fromJSON(getURL(URL, encoding = "latin1"))
I've also corrected some minor errors in your code: you forgot to request RCurl
, and paste0
was not needed.