Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Technique to avoid this parse error? JSON +double quote

We know in JSON string object we have property and its value as “Property”:”Value”

Suppose my Value contains a double quote, Like “Property”: “my country is “uk” ”

We know that this is going to give parse error on JSON.parse().

What is the technique to avoid this parse error?

like image 637
Kuttan Sujith Avatar asked Aug 01 '26 14:08

Kuttan Sujith


2 Answers

You can use backslash to escape the double qoute, read more about escape character over here.

Change

“my country is “uk” ”

To

“my country is \“uk\” ”
like image 184
Adil Avatar answered Aug 03 '26 03:08

Adil


If you're encoding an object into JSON, you can use JSON.stringify():

JSON.stringify({
    Property: 'my country is "uk"'
})
// {"Property":"my country is \"uk\""}

As you can see from above example, the notation \" is used to properly escape double quotes.

like image 42
Ja͢ck Avatar answered Aug 03 '26 04:08

Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!