From simon.urbanek at r-project.org  Sat Jun 13 01:05:45 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 12 Jun 2009 19:05:45 -0400
Subject: [Rd] Issues converting from JSON to R
In-Reply-To: <c2350ba40906121534w20944afcq5879dcb520f8ad41@mail.gmail.com>
References: <c2350ba40906121534w20944afcq5879dcb520f8ad41@mail.gmail.com>
Message-ID: <D5D5E388-F92A-47EF-B991-BE9E02E1722F@r-project.org>


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",
> FALSE, NA);
>

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
l=list(123)
names(l)=""

>> 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.

Cheers,
Simon


>
>
> 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
>
>



[Rd] Issues converting from JSON to R
Simon Urbanek