[Rd] Issues converting from JSON to R

Simon Urbanek simon.urbanek at r-project.org
Sat Jun 13 01:05:45 CEST 2009

On Jun 12, 2009, at 6:34 PM, Kynn Jones wrote:

> When converting from JSON to R it seems logical that a JSON array  
> would
> correspond to an "unnamed" R list, while a JSON object would  
> correspond to a
> "named" R list.  E.g.
> JSON: [1, 3.1415927, "foo", false, null]   => R: list(1, 3.1415927,  
> "foo",

note that NULL and NA are entirely different concepts - I don't think  
mapping NULL to NA is a good idea ... why don't you just use NULL?

> and
> JSON { "int": 1, "float": 3.1415927, "string": "foo", "logical":  
> false,
> "null": null }  => R: list(int=1, float=3.1415927, string="foo",
> logical=FALSE, null=NA)
> But I see at least a couple of problems with this scheme.  First,  
> how would
> one distinguish between the R versions of an empty JSON array (i.e.  
> [ ]),
> and an empty JSON object (i.e. { })?

Just add an attribute in that special case for one or the other. Or  
even better in general you can make JSON dictionaries (or arrays as  
well) a specific class (each) -- that would also help with future  
dispatch when processing such objects. Also note that specific arrays  
(scalar ones) are actually better mapped to vectors ...

> Second, JSON allows the empty key in an object (e.g., this is a  
> valid JSON
> object: { "": 123 }), but as far as I can tell, R does not allow the  
> empty
> string as a name in a named list:

That's not true, try

>> list(""=123)
> Error: attempt to use zero-length variable name

That has nothing to do with lists per se - the problem is the empty  
argument name in the call to the function `list`. However, you'll be  
creating the list programmatically so you won't run into that.


> Any suggestions for dealing with these edge cases would be much  
> appreciated!
> TIA!
> kynn
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list