Hello @kartik,
Use native JSON.stringify method. Works with nested objects and all major browsers support this method.
str = JSON.stringify(obj);
str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output.
console.log(str); // Logs output to dev tools console.
alert(str); // Displays output using window.alert()
Other examples.
obj = JSON.parse(str); // Reverses above operation (Just in case if needed.)
Use a custom JSON.stringify replacer if you encounter this Javascript error
"Uncaught TypeError: Converting circular structure to JSON"
Hope it helps!!
Thank you!!