JSON Parse Unexpected token h caused quotes inside string value

Viewed 860

I have a JSON string that I got from the table, here's my JSON

    {"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"}

when I'm trying to do json.parse() but I got an error

SyntaxError: Unexpected token h in JSON at position 154.

I thought it caused because of the quotes " in URL or colon :.

How do I can pass them to JSON?

Update

Here's how I got the data:

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

body_inbox = JSON.stringify(body_inbox);

I am confused, I found many data in table that data has double backslash in newline \\n and in url <a href=\\"https://test.com\\">here</a>. I just try create new data with JSON.stringify and the result is \n and <a href=\"https://test.com\">here</a>. Why does this happen?

Note

Sorry, there's a misleading the earlier data is typo.

It should be <a href=\\"https://test.com\\"> not <a href="\\https://test.com\\">

4 Answers

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))

(Posted an answer from the question author to move it to the answer space).

Thank you so much for all the answers. It's helped to figure out that the parse failed caused by double backslash that escape the second backslash not the double quotes ".

The data has double backslash because of this is data from cloned database, I thought it happen during conversion to SQL file.

Related