Coding stepping on the pit - MySQL updates the field storing JSON, \" escapes to "

This article introduces the problems encountered when executing MySQL online changes, which are manifested as "When updating the JSON field, the actual updated value is inconsistent with the value in the SQL statement, and the JSON format is wrong", as follows;

Problem Description

To deal with online problems, you need to insert a new record; paste the original JSON, modify some fields, and then submit the SQL update statement. The original JSON is as follows:

{
    "playerQualifyType": 1,
    "surveyId": 14,
    "playerRegisterEndTime": 1670860799000,
    "planName": "《碧蓝大陆》先锋测试招募!",
    "planStatus": 2,
    "playerRegisterStartTime": 1669860000000,
    "gameName": "碧蓝大陆",
    "developerName": "海南羽弘信息技术有限公司",
    "planStartTime": 1673316000000,
    "qualificationCodeUrl": "",
    "updateTime": 1669777099000,
    "planPlayerNum": 1500,
    "extend": "{\"innerDesc\":\"\",\"planId\":16,\"verifyOperator\":\"Akira-11126666\",\"verifyResultDesc\":\"\",\"verifyResultDescImgList\":[],\"verifyResultStatus\":2,\"verifyTime\":1669777099887}",
    "planEndTime": 1673884800000,
    "pkgStatus": 0
}

Now submit the SQL change order and execute the SQL statement as follows:

update t set `json_field` = 
'
{
    "playerQualifyType": 1,
    "surveyId": 14,
    "playerRegisterEndTime": 1670860799000,
    "planName": "《碧蓝大陆》先锋测试招募!",
    "planStatus": 2,
    "playerRegisterStartTime": 1669860000000,
    "gameName": "碧蓝大陆",
    "developerName": "海南羽弘信息技术有限公司",
    "planStartTime": 1673316000000,
    "qualificationCodeUrl": "",
    "updateTime": 1669777099000,
    "planPlayerNum": 1500,
    "extend": "{\"innerDesc\":\"\",\"planId\":16,\"verifyOperator\":\"Akira-11126666\",\"verifyResultDesc\":\"\",\"verifyResultDescImgList\":[],\"verifyResultStatus\":2,\"verifyTime\":1669777099887}",
    "planEndTime": 1673884800000,
    "pkgStatus": 0
}
'
where id = 55;

As a result, the records found after the update in the database were as follows, which did not meet the JSON format. After careful inspection, it was found that all the backslashes in the value of the extend field disappeared, and the JSON parsing failed ;

{
    "playerQualifyType": 1,
    "surveyId": 14,
    "playerRegisterEndTime": 1670860799000,
    "planName": "《碧蓝大陆》先锋测试招募!",
    "planStatus": 2,
    "playerRegisterStartTime": 1669860000000,
    "gameName": "碧蓝大陆",
    "developerName": "海南羽弘信息技术有限公司",
    "planStartTime": 1673316000000,
    "qualificationCodeUrl": "",
    "updateTime": 1669777099000,
    "planPlayerNum": 1500,
    "extend": "{"innerDesc":"","planId":16,"verifyOperator":"Akira-11126666","verifyResultDesc":"","verifyResultDescImgList":[],"verifyResultStatus":2,"verifyTime":1669777099887}",
    "planEndTime": 1673884800000,
    "pkgStatus": 0
}

Troubleshooting

Contact the DBA to describe the problem, and the reply is——

\" is equivalent to escaping into ", which is the result of MySQL's own processing ;

The experiment is as follows:

But the execution in the code is no problem, the reason is that the Java type corresponding to this field in the code is String, that is to say, the outer layer of the original JSON is surrounded by a layer of double quotation marks "", so there will be an embedding of an escape character set , as follows;

\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\"

Therefore, the MySQL insertion of the JSON string is executed in the code, and the actual value stored in the database is consistent with the expectation; the correct SQL is as follows:

update t set `json_field` = 
'
{\"playerQualifyType\":1,\"surveyId\":14,\"playerRegisterEndTime\":1670860799000,\"planName\":\"《碧蓝大陆》先锋测试招募!\",\"planStatus\":2,\"playerRegisterStartTime\":1669860000000,\"gameName\":\"碧蓝大陆\",\"developerName\":\"海南羽弘信息技术有限公司\",\"planStartTime\":1673316000000,\"qualificationCodeUrl\":\"\",\"updateTime\":1669777099000,\"planPlayerNum\":1500,\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\"planEndTime\":1673884800000,\"pkgStatus\":0}
'
where id = 55;

solution

When updating the JSON string field of the Mysql table, the steps should be as follows:

1. Enter the query statement, find the target record, copy the value of the JSON field to the JSON editing tool, and modify the attribute value in the JSON

2. Paste the modified JSON into the IDE, and the IDE accepts this string of JSON with double quotes;

3. When executing SQL, the value in the SQL statement is the new value in SQL in the string wrapped in double quotes in the IDE;

    // 将JSON粘贴进来 整体作为SQL更新语句中的新的value 这里也可以不格式化JSON
    String formatValue = "{\n" +
            "    \"playerQualifyType\": 1,\n" +
            "    \"surveyId\": 14,\n" +
            "    \"playerRegisterEndTime\": 1670860799000,\n" +
            "    \"planName\": \"《碧蓝大陆》先锋测试招募!\",\n" +
            "    \"planStatus\": 2,\n" +
            "    \"playerRegisterStartTime\": 1669860000000,\n" +
            "    \"gameName\": \"碧蓝大陆\",\n" +
            "    \"developerName\": \"海南羽弘信息技术有限公司\",\n" +
            "    \"planStartTime\": 1673316000000,\n" +
            "    \"qualificationCodeUrl\": \"\",\n" +
            "    \"updateTime\": 1669777099000,\n" +
            "    \"planPlayerNum\": 1500,\n" +
            "    \"extend\": \"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\n" +
            "    \"planEndTime\": 1673884800000,\n" +
            "    \"pkgStatus\": 0\n" +
            "}";
    
    // 未格式化 会少去/n换行符
    String value = "{\"playerQualifyType\":1,\"surveyId\":14,\"playerRegisterEndTime\":1670860799000,\"planName\":\"《碧蓝大陆》先锋测试招募!\",\"planStatus\":2,\"playerRegisterStartTime\":1669860000000,\"gameName\":\"碧蓝大陆\",\"developerName\":\"海南羽弘信息技术有限公司\",\"planStartTime\":1673316000000,\"qualificationCodeUrl\":\"\",\"updateTime\":1669777099000,\"planPlayerNum\":1500,\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\"planEndTime\":1673884800000,\"pkgStatus\":0}";

Guess you like

Origin blog.csdn.net/minghao0508/article/details/128372093