JSON.stringify returns "[object Object]" instead of the contents of the object

Anderson Green picture Anderson Green · May 11, 2013 · Viewed 102.4k times · Source

Here I'm creating a JavaScript object and converting it to a JSON string, but JSON.stringify returns "[object Object]" in this case, instead of displaying the contents of the object. How can I work around this problem, so that the JSON string actually contains the contents of the object?

var theObject = {name:{firstName:"Mark", lastName:"Bob"}};
alert(JSON.stringify(theObject.toString())); //this alerts "[object Object]"

Answer

Arbel picture Arbel · May 11, 2013

Use alert(JSON.stringify(theObject));