Re: [Rd] Issues converting from JSON to R

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Fri, 12 Jun 2009 19:05:45 -0400

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 12 Jun 2009 - 23:08:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 13 Jun 2009 - 01:35:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive