Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected character error when importing a json file into R

I am trying to load a local json file into R. I have tried the rjson and the RJSONIO package but I get the same error. E.g., with the rjson package I tried the following:

testdata<-fromJSON(file="testfile2.json",method="C",unexpected.escape="skip")

And it returns:

Error in fromJSON(file = "testfile2.json", method = "C", unexpected.escape = "skip") : 
unexpected character '<ff>

The json test file is a very simple file (I have a more complex file that I want to load once the error is remove):

{
"item1": "I love jquery4u",
"item2": "You love jQuery4u",
"item3": "We love jQuery4u"
}

Would be great if someone could tell what I am doing wrong. Thank you!

like image 738
Fred Avatar asked Dec 10 '25 20:12

Fred


1 Answers

It's almost certainly an encoding issue (as timelyportfolio suggested too). The unexpected character is consistent with the presence of, for example, the UTF-16 BOM (byte order mark) character.

If you run

f <- file("testfile2.json", "rb")
bytes <- readBin(f, integer(), n = 500, size = 1)
close(f)
bytes

you should get

 [1] 123  10  34 105 116 101 109  49  34  58  32  34  73  32 108 111 118 101
[19]  32 106 113 117 101 114 121  52 117  34  44  10  34 105 116 101 109  50
[37]  34  58  32  34  89 111 117  32 108 111 118 101  32 106  81 117 101 114
[55] 121  52 117  34  44  10  34 105 116 101 109  51  34  58  32  34  87 101
[73]  32 108 111 118 101  32 106  81 117 101 114 121  52 117  34  10 125  10

for the example json in your question and a file without funny characters.

If, on the other hand, there are "-1" or "255" in the output then the encoding is wrong and you'll have to open and re-save the original json file in an editor that allow you to specify the encoding.

like image 174
WhiteViking Avatar answered Dec 13 '25 08:12

WhiteViking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!