0

In Chrome console, I type:

JSON.stringify({a:{a:'{"a":"a"}'}})

I get the output:

"{"a":{"a":"{\"a\":\"a\"}"}}"

And I try to deserialize by:

JSON.parse('{"a":{"a":"{\"a\":\"a\"}"}}')

I get the error:

Uncaught SyntaxError: Unexpected token a(…)

How can I deserialize the original object?

enter image description here

1
  • 1
    if you're going to do it in the console, you have to escape " and \ like so JSON.parse("{\"a\":{\"a\":\"{\\\"a\\\":\\\"a\\\"}\"}}") Commented Oct 11, 2015 at 5:02

2 Answers 2

2

OK, I got the trick...

Escape the backslash '\', this works:

JSON.parse('{"a":{"a":"{\\"a\\":\\"a\\"}"}}')
Sign up to request clarification or add additional context in comments.

Comments

0

Just use from variables:

var str = JSON.stringify({
  a: {
    a: '{"a":"a"}'
  }
});
console.log(str); //{"a":{"a":"{\"a\":\"a\"}"}}
console.log(JSON.parse(str));  //original object
console.log(JSON.parse('{"a":{"a":"{\"a\":\"a\"}"}}')); //error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.