The
JSON.stringify()
will remove functions from a JavaScript object because
function
is not valid data type in
JSON
var obj = { name: "John", age: function () {return 30;}, city: "New York" };

// Functions and any other unsupported data types will be ignored
var myJSON = JSON.stringify(obj);
document.write(myJSON);
The result will be:
{"name":"John","city":"New York"}
As we can see, the
function
is removed. To solve this issue we can convert the function into
string
before running
stringify
var obj = { name: "John", age: function () {return 30;}, city: "New York" };
obj.age = obj.age.toString();
var myJSON = JSON.stringify(obj);
document.write(myJSON);
The result will be:
{"name":"John","age":"function () {return 30;}","city":"New York"}
by Valeri Tandilashvili
4 years ago
JSON
JavaScript
JS JSON
1
Pro tip: use ```triple backticks around text``` to write in code fences