How to append multiple entries to JSON Array in MySQL?

Viewed 293

I am using MySQL 8 and trying to update JSON data type in a mysql table

My table t1 looks as below:

# id    group   names   
1100000 group1  [{"name": "name1", "type": "user"}, {"name": "name2", "type": "user"}, {"name": "techDept", "type": "dept"}]

The JSON format has two types - user and dept

Now, I have a list of users userlist as below:

SET @userlist = '["user4", "user5"]';

I want to append @userlist to the JSON Array:

UPDATE t1 SET names = JSON_ARRAY_APPEND(names, '$', JSON_OBJECT('name', @userlist, 'type', 'user'))
WHERE `group` = 'group1';

The query is working but it is incorrectly adding data as below:

[{"name": "name1", "type": "user"}, 
{"name": "name2", "type": "user"},
{"name": "["user4", "user5"]", "type": "user"}
{"name": "techDept", "type": "dept"}]

Desired Output:

[{"name": "name1", "type": "user"}, 
{"name": "name2", "type": "user"},
{"name": "user4", "type": "user"},
{"name": "user5", "type": "user"},
{"name": "techDept", "type": "dept"}]
1 Answers
Related