JSON: How to create an undefined value or an NaN value?

Developers Kindergarten

Update 2020-FEB-07

At the present date and using Chrome version 80. The situation is this:

Thus two legal Java Script symbols (undefined and NaN) can not be made part of JSON formatted string. That is just the first peculiarity.

And note this: they can be created as text (as above), which in turn can not be parsed. That is the second peculiarity.

Even more troubling fact is: that kind of “naughty JSON” text can be sent over a wire between two distant systems. And you can be on the receiving side.

Can they be used as JSON in the javascript code?

Well, this is confusing. And I am not sure how I would solve this dichotomy as part of JSON spec? Is this already solved, I do not know?

So, was it much worse 7 years ago? Hint: no, it was not.

Update 2013-SEP-11

Or is the result of 1/0 ‘Infinity’ as this science called mathematics tells us? In any case and at this moment in time, and looking into this JSON conundrum, I started to think I had no idea.

[first published 2013JAN01]

Consider for example this little “number” :


Property “b” is simply and quietly not created, or so it seems? Perhaps it is me who is wrong or I need to go back to ES5 specification?

JSON NOT to rescue

Problem is not in some esoteric ES5 “feature” (aka omission) but it is an “issue” with JSON. JSON was created to ignore undefined (and functions, NaN’s and Infinity-es). And yes I knew this. I started this post in this way because I also wanted to show how this kind of “feature” can catch anybody off-guard. And repeatedly at that.

In the snippet above there is yet another fundamental rule of JavaScript/DOM programming broken:

Never use JSON.stringify() for debugging

Object literal above is not a mistake. It really produces an object with properties “a” and “b”, where “b” is indeed undefined. And crucially key “b” does exist, and its value is undefined. So, back to senses, hit F12 in your venerable Chrome or IE, and try this:

And voila, the output is the actual object created, with two properties present:

Not out of this wood, yet

This might be one happy ending to one blog post, but alas it is not. Far from that.

The big worm in that can is this: How is this reflected on AJAX? How are XMLHTTPREQUEST implementations out there handling this JSON “feature”? Namely: how is one passing a JSON object where some keys might have values as e.g. undefined? JSON encoded object like this one:

is of course NOT a good JSON and resident JSON.parse() method on the receiving side will throw a syntax error:

I am not even sure I can hope that the sending side will allow me to come to the above. That is I am not sure how to pack the offending object into the valid JSON string.

Valid requirement. Although 1/0 is 'Infinity'.
1/0 is ‘Infinity’. It is not ‘undefined’. This Wikipedia image is wrong.

So, what gives? I see no other way but to perhaps use ‘null’ instead of undefined’? But then I have no simple/native/natural way of JSON trustingly encoding the object which (for example) represents the calculation of the table from the image in this post ).

if x is zero then 1/x is not ‘null’. It is mathematically represented as ‘infinity’, not as ‘null’. And not as “undefined” either, as a table on the image is wrong. Strictly 1/0 is Infinity, but then JSON encoding rules do not allow that too: Infinity is not a legal value in JSON.

Undefined “is” null but its identity is not null … We are definitely not in the “good parts”  of JavaScript, a territory in here.

In pure JavaScript, I can compute NaN or Infinity values or hold undefined ones, but how do I express in JSON that something is undefined, Infinity or NaN? Beside putting it in a string: “undefined”, “NaN”, “Infinity”.

NaN, undefined and null is three distinctive values. And just like any other values in JavaScript

Solution? Direction? Perhaps, but solution no. If I have to “decode” these strings into values on the receiving side, this introduces yet another level of encoding of “special” values and that is not a good thing. Not at all.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

One thought on “JSON: How to create an undefined value or an NaN value?”