How to escape double quote in JSON string

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 107

How to escape double quote in JSON string

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?

MarPlo Posts: 186
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)