Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make rjson's fromJSON method convert JSON NULLs to R NAs?

Tags:

json

null

r

I could of course solve this downstream in R, but I think that would be messier compared to just get rjson to do it for me somehow. Can it be done?

like image 386
c00kiemonster Avatar asked Oct 11 '22 04:10

c00kiemonster


1 Answers

Two ideas:

  1. Take a look at RJSONIO instead, and use its fromJSON. The argument to look for is nullValue, which you can set to be NA. I switched from rjson to RJSONIO a long time ago, after doing some speed tests and it also produces somewhat more readable JSON.
  2. Consider reading in the text as a string, and replace 'null' with 'NA' using gsub(). This isn't particularly robust if you aren't familiar with regular expressions (if "null" is part of a bit of text, you could end up dropping it, so it's important to be careful).
like image 136
Iterator Avatar answered Oct 16 '22 04:10

Iterator