Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify(undefined) is not a string

JSON undefined errors

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?

like image 270
Web_Designer Avatar asked Jul 03 '13 16:07

Web_Designer


People also ask

Does JSON Stringify remove undefined?

JSON. stringify will omit all object attributes that are undefined .

Does JSON Stringify return string?

Return Value: It returns a string for a given value. Example: The below is the example of the JSON stringify() Method.

What Cannot be JSON Stringify?

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).

What causes JSON Stringify failure?

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.


1 Answers

undefined is not valid JSON, so the function is working properly.

http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example

like image 186
GJK Avatar answered Sep 30 '22 17:09

GJK