Home > OS >  jsonlite package not working to import file with ANSI encoding
jsonlite package not working to import file with ANSI encoding

Time:01-25

I'm trying to import a json file in R (the downloaded file encoding is ANSI.), but the error below appears:

> library(jsonlite)
> js <- url %>% 
    read_html() %>%
    html_text() 
> js <- jsonlite::fromJSON(js)
Error: lexical error: invalid bytes in UTF8 string.
          _name": "Av. Nossa Sra. de Fátima , 25",             "stop_i
                     (right here) ------^

Reproducible example:

url <- "https://jeap.rio.rj.gov.br/MOB/get_stops_details.rule?sys=MOB&INDICE=1"

library(rvest)
library(jsonlite)

js <- url %>% 
  read_html() %>%
  html_text() 

js <- jsonlite::fromJSON(js)
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252   
[3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C                      
[5] LC_TIME=Portuguese_Brazil.1252  

CodePudding user response:

Set the right encoding in the read_html

library(rvest)
library(jsonlite)

js <- url %>% 
  read_html(encoding = 'latin1') %>%
  html_text() 

js <- jsonlite::fromJSON(js)
  •  Tags:  
  • Related