Let's say I have an object variable:
var obj = {
key: '\"Hello World\"'
}
I then attempted to convert it to a string using JSON.stringify
in the Chrome devtools console:
JSON.stringify(obj) // "{"key":"\"Hello World\""}"
The result was "{"key":"\"Hello World\""}"
. Next, I assigned this as a string:
var str = '{"key":"\"Hello World\""}'
After that, I tried to convert it back to an object:
JSON.parse(str);
However, I encountered an error message saying Uncaught SyntaxError
I am confused as to why this is happening. I extracted the string from the original object and simply want to revert it back.
How can I resolve this issue? If my goal is to convert the object to a string and then return it back to its original form, what should I do?