1

I'm trying to import a JSON file using the jsonlite library.

When I run the fromJSON function, I get the following error -

df <- fromJSON("Inputfile.json",flatten = TRUE)


Error in parse_con(txt, bigint_as_char) : 
  lexical error: invalid char in json text.
                                       ÿþ[                     (right here) ------^

Any suggestions for a workaround? Thanks

sessionInfo():

R version 4.0.1 RC (2020-05-30 r78643) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 10 x64 (build 18363) 

Matrix products: default 

locale: [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 
[4] LC_NUMERIC=C LC_TIME=French_France.1252
5
  • Your error is slightly formatted somewhat unreadable. I am assuming the arrow points to the characters at the same line? Either way the problem is that it does not know how to read a specific Character. This might be a problem in your locale setting, or it might be a similar problem that Asian characters had from 4.0.0 to 4.0.4 (fixed in 4.0.5). Could you share more information such as your sessionInfo() and format your error accordingly? Commented Apr 7, 2021 at 20:45
  • R version 4.0.1 RC (2020-05-30 r78643) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363) Matrix products: default locale: [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 [4] LC_NUMERIC=C LC_TIME=French_France.1252 Commented Apr 7, 2021 at 20:51
  • It's strange that I'm getting this error because I've got another JSON that is structured exactly the same way as this file. And it imports fine. Commented Apr 7, 2021 at 20:51
  • Potentially a line ends in a null string \0. (Can happen unexpectedly). If that's the case reading the file as text txt <- scan(file) and removing those before reading it in as json fromJSON(gsub('\\0', '', txt)) might fix it. I'm not the most experienced in this specific problem, so it might not work, but surely someone will pop in with a suggestion if this is the case. Commented Apr 7, 2021 at 21:02
  • 1
    I think I got it. It was a UTF-16 encoded file. I saved as UTF-8 and re-ran the function. It worked! Commented Apr 7, 2021 at 21:08

1 Answer 1

1

Saved the file from UTF-16 to UTF-8 and the function worked.

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

Comments

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.