Hacker News new | ask | show | jobs
by cyphar 2334 days ago
"undefined" in this context means that the JSON object has had the relevant key omitted rather than explicitly set to null. This is valid JSON (it just so happens that JavaScript handles this scenario by returning "undefined" as with all undefined variables).
2 comments

Ah, right, my bad.

In my defense, JavaScript treats a value of `undefined` differently from an unset field / variable (e.g. when testing `object.hasOwnProperty('field')`).

JavaScript has too many "non-visible" value types in my opinion. This makes for both messy code and messy debugging.
Yes omitted value vs provided with null which is easy to work with in JS because of null and undefined and them being falsey.

Working with xml or json in more typed language like C# is less fun because there is not distinction in the language instead you end up using dom like checks to see if the key was provided or was set to null.

.Net code generator use to add an extra property called [property name]Specified for xml serialization so you could do null values or not send the node. Definitely prefer more direct language support for no value provided vs a null value.