3

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\\">

halfer
  • 19,824
  • 17
  • 99
  • 186
Blackjack
  • 1,016
  • 1
  • 20
  • 51

4 Answers4

2

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"
ray
  • 26,557
  • 5
  • 28
  • 27
  • yeah it's an escape string, but i dont know why it's escape backslash in url in my old data. I thought I only use JSON.stringify. It should only single backslash in url to escape the `"`. I dont know why there's an escape string backslash in url. – Blackjack Apr 02 '20 at 18:49
  • I've tried, if its a double stringify no matter different process or same, it will create `\\\"` to escape `\"` and `\\n` to escape `\n`. Now I found out why, this data is from cloned db. I've check to main db, it contain `\n` and `\"` – Blackjack Apr 02 '20 at 20:13
2

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"}
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
  • The answer is correct but I'd like to add more clarity regarding the escape-character typo. In the original json string the typo is the inclusion of the extra backslash which escapes the second backslash instead of escaping the double-quote. The code-snippet is lengthy and difficult to pinpoint the issue. – Reinsbrain Apr 02 '20 at 05:46
2

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))
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • 1
    That's misleading. This example "works" only because the backslash is also the escape character in template literals, thus involuntary producing valid JSON. I.e. writing `\`"\\""\`` will pass `"\""` to `JSON.parse`. However, the OP never specific how they are creating the data... – Felix Kling Apr 02 '20 at 06:08
  • `{ subtitle: 'Information', desc: 'Hi, Welcome.\n

    \n You can access our website here.\n

    \n Dont forget, cost: only $2!\n
    \n
    \n
    \n
    \n Thankyou,
    \n Regards' } `
    – xdeepakv Apr 02 '20 at 17:39
  • when i parse i see like this – xdeepakv Apr 02 '20 at 17:40
  • Yes, you all right. That's was misleading. The above example is right. – Blackjack Apr 02 '20 at 18:59
0

(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.

halfer
  • 19,824
  • 17
  • 99
  • 186