How to escape quotes in json?


When sending data to front-end from back-end, how can I pass JSON string with quotes, so how can I escape quotes in json? So I can pass JSON as below

"WeekDays": {
    "Sunday": {
        "text1": "Good morning",
        "text2": "Some more \"Example text\" here "
    }
}

Thanks


Asked by:- bhanu
0
: 1132 At:- 1/17/2023 9:09:02 AM
Javascript json escape quotes







1 Answers
profileImage Answered by:- vikas_jk

If you want to escape double quotes in JSON, you can simply use below JSON

'{"Text":"Hello \\"My Name\\" is Vikas"}'

Which will be converted into

'{"Text":"Hello "My Name" is Vikas"}'

Above example also works with JSON.Parse in Javascript.

var jsontext = '{"Text":"Hello \\"My Name\\" is Vikas"}'
console.log(JSON.parse(jsontext));
//output
//{
//  Text: "Hello \"My Name\" is Vikas"
//} 

OR

Use Raw strings

console.log(JSON.parse(String.raw`"Some example of double quote >> \" <<"`));

That's it, any of the above should work.

1
At:- 1/17/2023 9:15:38 AM
Thanks, that's what I was looking for. 0
By : bhanu - at :- 1/17/2023 9:18:18 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use