2

I am trying to read json object in R from file, which contains names and surnames in unicode. Here is the content of the file "x1.json":

{"general": {"last_name":
"\u041f\u0430\u0449\u0435\u043d\u043a\u043e", "name":
"\u0412\u0456\u0442\u0430\u043b\u0456\u0439"}}

I use RJSONIO package and when I declare the JSON object directly, everything goes well:

x<-fromJSON('{"general": {"last_name": "\u041f\u0430\u0449\u0435\u043d\u043a\u043e", "name": "\u0412\u0456\u0442\u0430\u043b\u0456\u0439"}}')
x
# $general
# last_name      name 
# "Пащенко" "Віталій" 

But when I read the same from file, strings are converted to some unknown for me encoding:

x1<-fromJSON("x1.json")
x1
# $general
#    last_name         name 
# "\0370I5=:>" "\022VB0;V9" 

Note that these are not escaped "\u" (which was discussed here)

I have tried to specify "encoding" argument, but this did not help:

> x1<-fromJSON("x1.json", encoding = "UTF-8")
> x1
$general
   last_name         name 
"\0370I5=:>" "\022VB0;V9" 

System information:

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

Switching to English (Sys.setlocale("LC_ALL","English")) has not changed the situation.

Community
  • 1
  • 1
Olga Makarova
  • 121
  • 2
  • 7
  • 1
    Is there any reason for you to not use `rjson` package? It works with its function `fromJSON`. –  Oct 09 '15 at 05:55
  • 1
    What @Pascal said, see http://stackoverflow.com/questions/30580601/how-to-correctly-deal-with-escaped-unicode-characters-in-rs-library-rjsonio-whe – Roman Luštrik Oct 09 '15 at 05:56
  • @Pascal, for me it does not, unfortunately (I forgot to mention this in the initial post): `x1<-RJSONIO::fromJSON("x1.json")` `> x1` `$general` ` last_name name ` `"\0370I5=:>" "\022VB0;V9" ` `> rm(x1)` `> x1<-rjson::fromJSON("x1.json")` `Error in rjson::fromJSON("x1.json") : unexpected character 'x'` `> x1` `Error: object 'x1' not found` – Olga Makarova Oct 11 '15 at 07:45
  • You need to read the help page. Syntax is different. –  Oct 11 '15 at 07:47
  • 1
    @Pascal, with `x1<-rjson::fromJSON(file="x1.json")` it worked, thank's a lot! – Olga Makarova Oct 11 '15 at 07:55

2 Answers2

1

If your file had unicode data like this (instead of its representation)

{"general": {"last_name":"Пащенко", "name":"Віталій"}}

then,

> fromJSON("x1.json", encoding = "UTF-8")

will work

If you really want your code to work with current file, try like this

JSONstring=""
con  <- file("x1.json",open = "r")
while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {
JSONstring <- paste(JSONstring,parse(text = paste0("'",oneLine, "'"))[[1]],sep='')
}
fromJSON(JSONstring)
MikA
  • 5,184
  • 5
  • 33
  • 42
  • It works with `fromJSON` from `rjson` package, without manipulation, as I said 3 hours ago. –  Oct 09 '15 at 09:53
0

use library("jsonlite") not rjson

library("jsonlite")
mydf <- toJSON( mydf, encoding = "UTF-8")

will be fine

  • the code ```toJSON``` creates a JSON file. It does not read one into R according to the [JSONlite description](https://cran.r-project.org/web/packages/jsonlite/jsonlite.pdf) – Simone Oct 07 '21 at 06:58