Home > front end >  How to retain escape characters after UNCOMPRESS in MySQL?
How to retain escape characters after UNCOMPRESS in MySQL?

Time:11-14

When I uncompress a compressed string that contains an escaped double quote charater, the resultant string does not contain the escape character ( \ ) itself. This is causing havoc with the subsequent call to JSONConvert.DeserializeObject

For example, if I call:

SELECT UNCOMPRESS(COMPRESS("Test\"Me"));

the expected result is

"Test\"Me"

but the actual result is

"Test"Me"

What should I do to get the result I want?

CodePudding user response:

If you want to get \" in the middle then you have to escape it twice in the COMPRESS() part. Something like this:

SELECT UNCOMPRESS(COMPRESS("Test\\\"Me"));

Or maybe, instead of wrapping the value in double-quotes, use single quote and do escaping once only on the backslash, something like this:

SELECT UNCOMPRESS(COMPRESS('Test\\"Me'));

Demo fiddle

CodePudding user response:

As I mentioned in the OP, the problem is when I use the result to Deserialize a JSON string. It throws an exception saying "Unrecognized token" since the string is now appearing like

{
  "fieldName": "This is a "double quote""
}

instead of

{
  "fieldName": "This is a \"double quote\""
}

Now do you see the problem? How can I escape it twice before compressing?

  • Related