Note that there are some explanatory texts on larger screens.

plurals
  1. POShould JSON null be preserved?
    text
    copied!<p>I tried to convert the JSON data</p> <pre><code>{ "a": { "b": null } } </code></pre> <p>to XML using <a href="http://jsontoxml.utilities-online.info/" rel="nofollow noreferrer">an online converter</a>. The response was</p> <pre><code>&lt;a&gt; &lt;b /&gt; &lt;/a&gt; </code></pre> <p>Converting this back to JSON using the same converter gave me</p> <pre><code>{ "a": { } } </code></pre> <p>This made me wonder – if you're explicitly given a null value, are you required to preserve it when dealing with JSON? I'm fairly sure that the XML <code>&lt;a&gt;&lt;b /&gt;&lt;/a&gt;</code> is <em>not</em> equivalent to <code>&lt;a&gt;&lt;/a&gt;</code>, and especially not <code>&lt;a /&gt;</code> (which happens to be what I get when I continue with the same exercise).</p> <p>In other words, if I'm handed JSON of unknown origin and am supposed to hand it over to an unknown recepient, am I required to preserve the nulls or can I safely remove them? Conversely, can I rely on my nulls to end up in the same way I outputted them when delivered by third-party software?</p> <p>Here's a similar question: <a href="https://stackoverflow.com/questions/11003424/should-json-include-null-values">Should JSON include null values</a> – However, the question there is whether the code should output nulls if <em>you define the format</em> yourself, not what you should do if you don't know anything about the original format.</p> <p><strong>EDIT – Clarification:</strong> The way I asked the question was bad and apparently caused confusion. To rephrase it: I <em>do</em> understand that XML and JSON are different formats and are able to carry different kinds of (meta)data. I <em>do</em> know that <strong>null</strong> is a valid value, as defined by <a href="http://www.ietf.org/rfc/rfc4627.txt?number=4627" rel="nofollow noreferrer">RFC4627</a>. I <em>do</em> understand that there are different ways to convert between XML and JSON since the formats don't have a 1-to-1 relationship. I <em>do</em> understand that the converter I found might be buggy. However, the fact that the <em>same</em> converter didn't provide the same conversion in both directions (no information was lost when converting from <code>"b": null</code> to <code>&lt;b /&gt;</code> and a similar translation in the opposite direction would have been possible) made me wonder something that I couldn't find an answer to despite attempts:</p> <ul> <li>Is it legal, according to the JSON standard, to treat <code>{"a":{"b":null}}</code> and <code>{"a":{}}</code> as one and the same object when transferring them on behalf of other software?</li> </ul> <p>Note that I'm here assuming that it's legal to add or remove whitespace as I see fit (e.g. pretty-printing, which is okay according to RFC4627), and even to rearrange the name/value pairs in a collection (again according to RFC4627). I simply don't know if a <strong>null</strong> must be preserved in the same way as significant data, or can be dropped in the same way as insignificant whitespace.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload