Bug #117526 MySQL incorrectly parses JSON strings containing JSON arrays, altering stored JSON format
Submitted: 20 Feb 5:58 Modified: 20 Feb 6:59
Reporter: yu cao Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:8.0.28 OS:CentOS
Assigned to: MySQL Verification Team CPU Architecture:x86
Tags: JsonParser

[20 Feb 5:58] yu cao
Description:
When inserting or selecting JSON formatted strings containing JSON arrays in a TEXT or MEDIUMTEXT column, MySQL automatically parses and modifies the stored format. This causes inconsistency between the stored data and the retrieved data.

Expected behavior:
SELECT '{"remark":"[\"1\",\"2\",\"3\"]"}';

Should return:
{"remark":"[\"1\",\"2\",\"3\"]"}

Actual behavior:
{"remark":["1","2","3"]}

The original JSON structure is altered. The value of remark is stored as a JSON string but is retrieved as a JSON array. This does not happen when the JSON string contains a JSON object ({"a":"b"}), only when it contains a JSON array (["1","2","3"]).

This is problematic because:

The database returns data in a different format than stored.
MySQL treats JSON arrays and objects inconsistently.
The parsed output is not valid JSON anymore according to JSON specification (RFC 8259).

How to repeat:
Reproducible Test Case

SELECT '{"remark":"[\"1\",\"2\",\"3\"]"}';

Expected:
{"remark":"[\"1\",\"2\",\"3\"]"}

Actual:
{"remark":["1","2","3"]}

Suggested Fix
MySQL should not automatically parse JSON strings unless explicitly requested by the user.
JSON strings should always be treated as plain text unless using JSON_UNQUOTE().
Consistency should be maintained between JSON objects {} and JSON arrays [].

Suggested fix:
- The issue is not that MySQL is parsing JSON, but that MySQL formats JSON in a way that makes it invalid.

- A JSON array stored as a string should remain a string when queried, not be converted into an array that breaks JSON validity.

- This behavior is inconsistent because it happens only with JSON arrays, but not with JSON objects.

- Fixing this behavior would prevent data corruption and ensure consistency in JSON parsing.
[20 Feb 6:59] MySQL Verification Team
This is not a bug.

\" is a way to escape char in the mysql client, has nothing to do with json.

e.g.

mysql> select '\"xyz';
+------+
| "xyz |
+------+
| "xyz |
+------+
1 row in set (0.00 sec)

mysql> 

If you want to write the escape in the field you have to escape it:

mysql> select '\\"xyz';
+-------+
| \"xyz |
+-------+
| \"xyz |
+-------+
1 row in set (0.00 sec)

mysql> 

Thank you for using MySQL Server