{"id":1243,"date":"2011-05-30T20:21:18","date_gmt":"2011-05-31T01:21:18","guid":{"rendered":"http:\/\/www.wwco.com\/~wls\/blog\/?p=1243"},"modified":"2011-05-30T20:21:18","modified_gmt":"2011-05-31T01:21:18","slug":"json-and-undefined-properties","status":"publish","type":"post","link":"https:\/\/wls.wwco.com\/blog\/2011\/05\/30\/json-and-undefined-properties\/","title":{"rendered":"JSON and Undefined Object Properties"},"content":{"rendered":"<p>When are <tt>JSON.parse()<\/tt> and <TT>JSON.stringify()<\/TT> not inverses of each another?<\/p>\n<p>Here&#8217;s when.<\/p>\n<p>In JavaScript it&#8217;s possible to define an object that has an undefined property:<\/p>\n<pre><code>var o = {\n    a: \"Hello\",\n    b: undefined\n};<\/code><\/pre>\n<p>It&#8217;s then possible to iterate over all the objects properties and see the values as well.<\/p>\n<pre><code>for (var p in o) {\n    alert( p + \": \" + o[p] );\n}<\/code><\/pre>\n<p>This will show &#8220;a: Hello&#8221; and &#8220;b: undefined&#8221;.<\/p>\n<p>Now, to JSON-ify the object and reconstitute it back.<br \/>\n<code>o = JSON.parse( JSON.stringify(o) );<\/code><\/p>\n<p>Doing the for-loop again will reveal the object now only has the &#8220;a&#8221; property (the value is still &#8220;Hello&#8221;).<\/p>\n<p><A TARGET=\"_blank\" HREF=\"http:\/\/jsbin.com\/ohoka4\/edit\" TITLE=\"Hit the PREVIEW button once there.\">Try it.<\/A><\/p>\n<p>While I get it, I&#8217;m not sure I like it.  Ideally, I&#8217;d like anything coming out of <a href=\"http:\/\/www.json.org\/\">JSON<\/a> to be the same as anything going into it.<\/p>\n<p>Assume this object:<\/p>\n<pre><code>var me = {\n    mySiblingsGender: undefined\n};<\/code><\/pre>\n<p>This is one of those cases where several subtleties rear their head on edge cases.<\/p>\n<p>First, a known value for <code>mySiblingsGender<\/code> might be <code>male<\/code> or <code>female<\/code>.  A <code>null<\/code> would indicate there is no gender, perhaps by a horrible <A href=\"http:\/\/www.wwco.com\/~wls\/blog\/2006\/11\/11\/an-open-letter-to-women-everywhere\/\">vegetable peeler accident<\/a>.  (Oh my!)  But an <em>undefined<\/em> value indicates there is one, we just don&#8217;t know what it is.<\/p>\n<p>Removing the property completely instead conveys that there is no sibling (absence) &mdash; something totally different than null (not having any) or undefined (unknown).<\/p>\n<p>While it&#8217;s splitting hairs, if not kindling for religious coding wars, it&#8217;s useful to know that JSON-out may not always be the same as JSON-in.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ran into an interesting case where the JavaScript object coming out of JSON wasn&#8217;t the same as the JavaScript object encoded going in.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,128,86],"tags":[345,354,355,373,406,407,572,578,633,634,716,736,852,853,903,969],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-geek","category-javascript","category-programming","tag-data","tag-decode","tag-decoding","tag-deserialize","tag-encode","tag-encoding","tag-javascript-2","tag-json","tag-marshal","tag-marshaling","tag-parse","tag-pickle","tag-serialization","tag-serialize","tag-stringify","tag-unpickle"],"_links":{"self":[{"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/comments?post=1243"}],"version-history":[{"count":0,"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:attachment":[{"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wls.wwco.com\/blog\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}