3

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!

3
  • Since the error is "escape" related, have you tried making your JSON file one line? Just in case it's a line-ending character issue. Commented Sep 8, 2015 at 15:20
  • have you tried jsonlite? what is the encoding of the file? Commented Sep 8, 2015 at 15:29
  • Thanks for your help. WhiteVikings answer solved the problem! Commented Sep 9, 2015 at 7:37

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You are awesome! It worked. I changed it to UTF-8 in an editor and now the error is gone.
Do you have any recommendation for a free resource where I can learn more about encoding so that in the future I can figure that out by myself?
@Fred None that I can think of. But one way or another you'll have to look at the actual bytes in the file. There's several free dedicated hex editors that can help there. Some editors can display as hex too. And so can simple command line tools like od on Mac or Unix). You also need some basic understanding of the difference between, say ASCII, UTF8 and UTF16, and may want to know what BOM markers are. That's enough to troubleshoot typical encoding issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.