Sample Header Ad - 728x90

How to concatenate multiple JSON arrays into a single array in MariaDB?

1 vote
2 answers
145 views
I'm working with a MariaDB database where I have a table with two columns: id and values, where values contains JSON arrays. Here is a simplified example of the data: | id | values | |----|-------------| | 1 | "[1, 2, 3]" | | 1 | "" | | 2 | "" | I want to group the rows by id and concatenate the JSON arrays in the values column into a single array, like this: | id | values | |----|---------------| | 1 | "[1, 2, 3, 5]"| | 2 | "" | I tried using JSON_ARRAYAGG but it creates an array of arrays, and I can't figure out how to merge the arrays into one. How can I achieve this in MariaDB?
SELECT id, JSON_ARRAYAGG(values)
  FROM REC
GROUP BY id
Bonus question: How to output only unique values?
Asked by Emax (111 rep)
Dec 16, 2024, 01:48 PM
Last activity: Aug 5, 2025, 01:04 PM