JSON.stringify(null)
returns the string null
.
JSON.stringify(undefined)
returns the value undefined
. Shouldn't it return the string undefined
?
Parsing the value undefined
or the string undefined
gives a SyntaxError
.
Could someone explain why JSON chokes on undefined
and how to get around it when stringifying / parsing values?
JSON. stringify will omit all object attributes that are undefined .
Return Value: It returns a string for a given value. Example: The below is the example of the JSON stringify() Method.
undefined , Function , and Symbol values are not valid JSON values. If any such values are encountered during conversion, they are either omitted (when found in an object) or changed to null (when found in an array).
JSON. stringify() throws an error when it detects a cyclical object. In other words, if an object obj has a property whose value is obj , JSON. stringify() will throw an error.
undefined
is not valid JSON, so the function is working properly.
http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With