0

I tried to run this SQL in MariaDB - 10.4.22-MariaDB:

SELECT CONCAT( '[', GROUP_CONCAT( JSON_ARRAY( name ) ), ']' ) FROM `mytable`;

But I'll get this:

[["ith1,"],["ith2"],..]

I need to get it without the brackets for each ithem, so I'll get just basic array:

["ith1,","ith2",..]

I also tried to use just GROUP_CONCAT, but if the separator is also in text, the whole array is then broken.

Example data:

ID Name
1 Football, boys "first group"
2 Football, girls "first group"
3 Streetball

Thanks!

5
  • Can you add some data examples ? Commented Mar 9, 2022 at 20:01
  • @ErgestBasha added Commented Mar 9, 2022 at 20:12
  • Use JSON_ARRAYAGG(). Commented Mar 9, 2022 at 20:23
  • @Barmar I'm using old version of MariaDB. JSON_ARRAYAGG() was unfortunately added in MariaDB 10.5.0. mariadb.com/kb/en/json_arrayagg Commented Mar 9, 2022 at 20:26
  • I edited to make it clear this is MariaDB, not MySQL. MariaDB forked from MySQL a long time ago, and it should not be considered as a compatible product anymore. Commented Mar 9, 2022 at 20:32

1 Answer 1

2

Use JSON_QUOTE() in GROUP_CONCAT()

SELECT CONCAT('[', GROUP_CONCAT(JSON_QUOTE(name)), ']') FROM test3;

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.