Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R does not show special characters coming from json source

a little bit about the background. I pull data from an API that supplies public transportation data. It returns the result in json format, which I process with the library 'jsonlite'.

 resp <- GET(url = url)


  resp_char <- rawToChar(resp$content)
  parsed <- fromJSON(resp_char, flatten = T)

  parsed.df <- do.call(what = "rbind", args = lapply(parsed[1], as.data.frame))

The problem is, in the result there are no special characters.

I am working on a Windows Server 2012 machine and my language settings in R look like this:

    > Sys.getlocale()
[1] "LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252"

Example:

    > df$direction
"U Alt-Mariendorf (Berlin)" 
"U Alt-Tegel (Berlin)" 
"U Alt-Mariendorf (Berlin)"              
"U Alt-Tegel (Berlin)" 
"Märkisches Viertel, Wilhelmsruher Damm"

The expected result for the fifth result is "Märkisches Viertel, Wilhelmsruher Damm"

After that I looked in the actual encoding.

> Encoding(df$direction)
   [1] "unknown" "unknown" "unknown" "unknown" "UTF-8"

In my opinion this looks good so far, but nevertheless I cannot see special characters.

I appreciate any suggestions and ideas on the subject.

Regards

like image 753
ahLoco Avatar asked Oct 30 '25 15:10

ahLoco


1 Answers

So finally I got it. Thanks to @parth, it has led me to the right answer. I used Encoding before my fromJSON statement and that worked for me.

  resp <- GET(url = url)

  resp_char <- rawToChar(resp$content)
  Encoding(resp_char) <- "UTF-8"
  parsed <- fromJSON(resp_char, flatten = T)

  parsed.df <- do.call(what = "rbind", args = lapply(parsed[1], as.data.frame))
like image 108
ahLoco Avatar answered Nov 02 '25 06:11

ahLoco