The R Project SVN R-packages

Rev

Blame | Last modification | View Log | Download | RSS feed

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSON, null and NA</title><link rel="stylesheet" type="text/css" href="../OmegaTech.css"></link><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"></meta><script xmlns="" type="text/javascript" src="http://www.omegahat.org/DynDocs/JavaScript/toggleHidden.js"></script>
</head><body class="yui-skin-sam">
<script xmlns="" type="text/javascript"><!--
var toggleCodeIds = [
 
   "idp6490400", 
   "idp6491040", 
   "idp6492208", 
   "idp6493232", 
   "idp6495152", 
   "idp6496864"
];
--></script><p xmlns=""></p>
<div class="article" title="JSON, null and NA"><div class="titlepage"><div><div><h2 class="title"><a id="idp20448"></a>JSON, null and <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b></h2></div><div><div class="author"><h3 class="author"><span class="firstname">Duncan</span> <span class="surname">Temple Lang</span></h3><div class="affiliation"><span class="orgname">University of California at Davis<br></br></span> <span class="orgdiv">Department of Statistics<br></br></span></div></div></div></div><hr></hr></div><div class="section" title="Limitations of JSON regarding the meaning of null"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idp6484160"></a>Limitations of JSON regarding the meaning of null</h2></div></div></div><p>
JavaScript Object Notation (JSON) is a convenient format for
representing data and facilitates transferring data between
applications. It is widely used in different applications, Web
services and other contexts.  As such, it is useful for R to be able
to import and export data in this format.  Unfortunately, JSON is a
little too simple and cannot faithfully represent all of the types and
values in R.  Most specifically, there is no way to support <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b>,
<b xmlns="" class="$" title="">Inf</b> or <b xmlns="" class="rkeyword">NaN</b>.  Typically, these
values are represented in JSON as "null".  However, that is also used
to represent a null object.  So there is ambiguity in how we interpret
null in JSON. 
We don't know whether it is <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b>, NaN, Inf or <i xmlns=""><code>NULL</code></i> in R.
This many-to-one mapping results in a loss of information.
</p><p>
In spite of the shortcomings of the format, we can still work with 
JSON. However, how we convert null values to R and how we
convert <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b> values from R is not automatic and uniquely defined.
For that reason, the caller must control how these are mapped.
We provide some mechanisms to do this in the 
<i xmlns="" class="rfunc"><a href="Help//fromJSON.html" title="">fromJSON()</a></i> and <i xmlns="" class="rfunc"><a href="Help//toJSON.html" title="">toJSON()</a></i> functions.
</p><p>
When converting R objects to JSON via <i xmlns="" class="rfunc"><a href="Help//toJSON.html" title="">toJSON()</a></i>,
one can specify how to map <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b> values to JSON.
One provides a value for the parameter <i xmlns="" class="rarg">.na</i>
to control this. 
For example, suppose we want to transform the R list
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6490400"><div><pre class="rcode" title="R code">
x = list(1, 2, NA, c(TRUE, NA, FALSE))
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
to JSON and want <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b> values to map  to null.
We can achieve this with
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6491040"><div><pre class="rcode" title="R code">
toJSON(x, .na = "null")
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
</p><p>
In some applications, we represent a missing value with a fixed number that is unlikely
to occur in actual data, e.g. -99999.
We can map <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b> values to such a number with
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6492208"><div><pre class="rcode" title="R code">
toJSON(list(1, 2, list(NA)), .na = -99999)
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
</p><p>
Now consider  round-tripping <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b>, e.g.
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6493232"><div><pre class="rcode" title="R code">
o = toJSON ( NA )
<pre class="routput">
[1] "[ null ]"
</pre>
fromJSON( o )
<pre class="routput">
[[1]]
NULL
</pre>
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
So we have lost information.
</p><p>
We can correct this loss of information by
specifying how to map null values in JSON
to R values. We use the <i xmlns="" class="rarg">nullValue</i>
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6495152"><div><pre class="rcode" title="R code">
fromJSON( toJSON ( NA ), nullValue = NA)
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
Again, here we as the caller of <i xmlns="" class="rfunc"><a href="Help//fromJSON.html" title="">fromJSON()</a></i>
(and also <i xmlns="" class="rfunc"><a href="Help//toJSON.html" title="">toJSON()</a></i>) we are providing
information about how to transfer the null value from JSON to R.
Only we know what it means in this case.
If we knew that the null corresponded to <b xmlns="" class="$" title="">Inf</b>,
we could  specify that: 
</p><div xmlns="" class="codeToggle"><div class="unhidden" id="idp6496864"><div><pre class="rcode" title="R code">
 fromJSON( "[null]", nullValue = Inf)
</pre></div></div></div>
<div xmlns="" class="clearFloat"></div>
<p>
</p><p>
Where this mechanism breaks down is when we have multiple
null values in our JSON content and they map to different
R values, e.g. <i xmlns=""><code>NULL</code></i>, <b xmlns:r="http://www.r-project.org" xmlns:s="http://cm.bell-labs.com/stat/S4" xmlns:py="http://www.python.org" xmlns:perl="http://www.perl.org" xmlns:c="http://www.C.org" xmlns:vb="http://www.visualbasic.com" xmlns:omegahat="http://www.omegahat.org" xmlns:bioc="http://www.bioconductor.org" xmlns:java="http://www.java.com" xmlns:sql="http://www.sql.org" xmlns=""><a href="library/base/html/NA.html">NA</a></b> and NaN.
The <i xmlns="" class="rarg">nullValue</i> parameter is a global replacement for
null entries in the JSON.  To adaptively process these null 
entries in a context specific manner, we have to use a customized
parser.  We can do this by providing an R function as the
callback handler for the JSON parser.
</p></div></div></body></html>