JSON Parse Unexpected token h caused quotes inside string value JSON Parse Unexpected token h caused quotes inside string value json json

JSON Parse Unexpected token h caused quotes inside string value


The double backslashes should be single backslashes. A single backslash escapes the following character, so what you’re doing with the double is escaping the second backslash. It chokes on the href because the quote that follows ends the string, after which the parser hits the ‘h’ in the url as a raw character.

{ message: "...our website <a href=\\"https://test.com\\">here</a>" }//                                   ^ parser thinks the string ends here//                                     and doesn't know what to make of//                                     https://...

My guess is that the data got escaped twice by two different processes (or the same process run twice).

Hypothetical example: The data gets created, and on the way into the database it gets escaped. So now all quotes are preceded by a backslash. Then an edit is made to the data and the record is updated in the database, and the escaping gets run again. But the input string already has backslashes in it from the first time, and because backslashes themselves are special and need to be escaped, when the string gets escaped (again) on its way back to the database you end up with double backslashes.

You can see this kind of thing by escaping a string twice in the console. (This isn't doing backslashes, but it's demonstrative of the problem):

const input = '"This string is quoted."';const once = encodeURI(input);// encodes the quotes as '%22'// "%22This%20string%20is%20quoted.%22"const twice = encodeURI(once);// encodes the '%' in '%22' as '%25', and you end up with `%2522`// "%2522This%2520string%2520is%2520quoted.%2522"


Double quotes in json (in value) only require one forward slash(\).So your json should be

{"subtitle":"Information","desc":"Hi, Welcome.\\n                            <br><br>\\n                            You can access our website <a href=\"https://test.com\">here</a>.\\n                            <br><br>\\n                            Dont forget, cost: only $2! \\n                            <br>\\n                            <br>\\n                            <br>\\n                            <br>\\n                            Thankyou,<br>\\n                            Regards"}


Use the template string to save data.

const json = `{"subtitle":"Information","desc":"Hi, Welcome.\\n<br><br>\\nYou can access our website <a href=\\"https://test.com\\">here</a>.\\n<br><br>\\nDont forget, cost: only $2!\\n<br>\\n<br>\\n<br>\\n<br>\\n Thankyou,<br>\\n Regards"}`console.log(JSON.parse(json))