Scenario: MySQL needs to fetch the value of the json field like '{pfAutoFinishStatus": "","shine":""Very good"","submitTime":"2023-04-05"}', because the value corresponding to shine There are two double quotes, resulting in a null value and interfering with the subsequent submitTime, resulting in a null value. How to get the value of shine: "very good"?
expected outcome:
Analysis: The value corresponding to shine has two double quotes. Generally, when encountering a special character, one must think of escaping it, so is it possible to use escaping? Next we verify.
1. There are no double quotes, but a normal double quote: '{ "pfAutoFinishStatus": "", "shine": "Very good", "submitTime": "2023-04-05" }'.
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "挺好", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE1;
2. There are two double quotes: '{ "pfAutoFinishStatus": "", "shine": ""Very good"", "submitTime": "2023-04-05" }'
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": ""挺好"", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE2;
3. There are two double quotes plus escape: '{ "pfAutoFinishStatus": "", "shine": "\\"Very good\\"", "submitTime": "2023-04-05" }'
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "\\"挺好\\"", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE3;
The above results are still different from the expected data. Then adjust
select JSON_UNQUOTE(JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "\\"挺好\\"", "submitTime": "2023-04-05" }', '$.shine')) as JSON_VALUE4;
Knowledge reserve:
1. JSON_EXTRACT function: Description of json_extract function in MySQL
2. JSON_UNQUOTE function: Description of json_extract function in MySQL