1
SET @v = '{"a":" \\u3b7e - c:\\user\\abc - \\"ET\\" "}';

SET @j = CAST(@v AS JSON);

failed Error Code: 3141. Invalid JSON text in argument 1 to function cast_as_json: "Incorrect hex digit after \u escape in string." at position 18. because cast_as_json will try to decode '\user' which is not a escape character.

if it like this c:\\\\user\\\\abc, everything fine.

Tried SET @v = REPLACE(@v, '\\', '\\\\');, it will break other string escape.

Tried SET @v = CONVERT(@v USING ASCII);, No matter what charset, no different

Mysql rule '\\' -> '\', '\u3b7e' -> '㭾'. Json rule " -> " in string

I don't know how to solve this.

4
  • 1
    You can't fix it up after the fact. The JSON has to be given correctly in the input data. Otherwise, there's no way to know which \\u is supposed to be literal versus starting a Unicode escape sequence. Commented Jan 16 at 20:26
  • Exactly, That is my problem. Commented Jan 16 at 20:45
  • 1
    You need to fix it at the source. This is a hopeless task. Commented Jan 16 at 20:50
  • Mysql rule ... '\u3b7e' -> '㭾'. ??? dbfiddle.uk/4jaPs9EU Commented Jan 17 at 4:32

1 Answer 1

0

Finally, I found the root cause is: In JSON, a Unicode surrogate pair must consist of two valid sequences: High surrogate: \uD800-\uDBFF Low surrogate: \uDC00-\uDFFF MySQL interprets JSON strictly and will throw an error if: The surrogate pair is incomplete or malformed. The string contains improperly escaped sequences.

and I just replaced \ -> \\, which will make '\"', separately '\\' and '"', create error.

Finally I replace all possible unicode surrogate to 0000. so far no more fail. Hope helps other people.

SET @jsn = REGEXP_REPLACE(@jsn, '\\\\u[dD][0-9a-fA-F]{3}', '\\\\uD000');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.