Bug #108231 JSON_ARRAY_APPEND adds backslashes when a JSON_OBJECT passed as variable
Submitted: 23 Aug 2022 5:15 Modified: 23 Aug 2022 7:30
Reporter: Derek McKinnon Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:5.7.37, 5.7.39, 8.0.30 OS:Any
Assigned to: CPU Architecture:Any
Tags: JSON_ARRAY_APPEND, JSON_OBJECT

[23 Aug 2022 5:15] Derek McKinnon
Description:
Passing a JSON_OBJECT() result as a value arg in a variable to JSON_ARRAY_APPEND() adds backslashes, but passing it directly does not. Easy to conform that this so both for user vars and for declared vars in stored routines.

How to repeat:

SET @i = JSON_ARRAY(JSON_OBJECT('a', '1', 'b', '2'));
SET @j = JSON_OBJECT('c', '3', 'd', '3');
SET @a = IF( @i IS NULL OR JSON_TYPE(@i) != 'ARRAY',
             JSON_ARRAY_APPEND( JSON_ARRAY(), '$', @j ),
             JSON_ARRAY_APPEND( @i, '$', @j )
           );
SET @b = IF( @i IS NULL OR JSON_TYPE(@i) != 'ARRAY',
             JSON_ARRAY_APPEND( JSON_ARRAY(), '$', @j ),
             JSON_ARRAY_APPEND( @i, '$', JSON_OBJECT('c', '3', 'd', '3'))
           );
SELECT @j, @a, @b\G
*************************** 1. row ***************************
@j: {"c": "3", "d": "3"}
@a: [{"a": "1", "b": "2"}, "{\"c\": \"3\", \"d\": \"3\"}"]
@b: [{"a": "1", "b": "2"}, {"c": "3", "d": "3"}]

Suggested fix:
Workaround is to always use the text within a JSON object rather than the JSON Object itself.  

See discussion at  https://forums.mysql.com/read.php?176,705914,705914#msg-705914
[23 Aug 2022 7:30] MySQL Verification Team
Hello Derek,

Thank you for the report and feedback.

regards,
Umesh