Mihai Vinaga
Mihai Vinaga

Reputation: 1109

MySQL 8 split string by comma and convert it into JSON ARRAY

I have the following string: "a,b,c,d" and I want to convert it into a json array, something like this ["a","b","c","d"] is there any MySQL 8 function that can achieve this?

Upvotes: 3

Views: 4265

Answers (2)

wchiquito
wchiquito

Reputation: 16551

Try:

SELECT
  CAST( 
    CONCAT('["', REPLACE('a,b,c,d', ',', '","'), '"]')
    AS JSON
  );

See dbfiddle.

Upvotes: 6

Dave Stokes
Dave Stokes

Reputation: 823

select json_array("a,b,c,d");
+-----------------------+
| json_array("a,b,c,d") |
+-----------------------+
| ["a,b,c,d"]           |
+-----------------------+

Upvotes: -4

Related Questions