Page 1 of 1

How to escape double quote in JSON string

Posted: 06 Dec 2020, 06:52
by Marius
I am trying to parse a JSON string 'Hello " test' containing double quote which already escaped.

Code: Select all

JSON.parse('{"x":"Hello \" test "}')
But I get error:

Code: Select all

Uncaught SyntaxError: Unespected Tocken t in JSON
Is there anything I missed here?

How to escape double quote in JSON string

Posted: 06 Dec 2020, 08:45
by MarPlo
You just need to escape the backslash \, so it turns into two backslashes \\

Code: Select all

let obj = JSON.parse('{"x":"Hello \\" test"}')
console.log(obj)