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

走远了吗. 提交于 2021-02-10 05:54:33

问题


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?


回答1:


Try:

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

See dbfiddle.




回答2:


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


来源:https://stackoverflow.com/questions/56958056/mysql-8-split-string-by-comma-and-convert-it-into-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!