The JSON spec does not allow undefined
values, but does allow null
values.
You can pass a replacer function to JSON.stringify
to automatically convert undefined
values to null
values, like this:
var string = JSON.stringify(
obj,
function(k, v) { return v === undefined ? null : v; }
);
This works for undefined values inside arrays as well, as JSON.stringify
already converts those to null
.