如何将JSON对象行转换为MySQL中的JSON数组?
卡在一个锻炼任务中,我想要json
对象的形式为json
array。实际上,我已经达到了第一阶段,将事物转换为JSON
对象。
第二阶段是我的输出应与下面的预期输出一样相同,但并非如此。
当前输出:
{"name": "Peter", "uniqueId": "1"}
{"name": "MaryChan", "uniqueId": "3"}
预期输出:
[{"name": "Peter", "uniqueId": "1"}, {"name": "MaryChan", "uniqueId": "3"}]
查询:
SELECT JSON_OBJECT('uniqueId', uniqueId, 'name', name) actors
FROM (
select stf.id as familyId, stl.skill_type_name as name
from actor_family af, actor_layered al
where af.id = al.actor_family_id) AS actors
GROUP BY uniqueId;
有些人建议使用group_concat
将其完成,但我仍然无法实现预期格式。
欢迎任何帮助或指针。
Stuck in one exercise task where I want JSON
objects to be in form of a JSON
array. Actually I have reached up to 1st stage to convert things into JSON
object first.
Second stage is where my output should be as same as expected output as below, but it is not coming.
Current output :
{"name": "Peter", "uniqueId": "1"}
{"name": "MaryChan", "uniqueId": "3"}
Expected output :
[{"name": "Peter", "uniqueId": "1"}, {"name": "MaryChan", "uniqueId": "3"}]
Query :
SELECT JSON_OBJECT('uniqueId', uniqueId, 'name', name) actors
FROM (
select stf.id as familyId, stl.skill_type_name as name
from actor_family af, actor_layered al
where af.id = al.actor_family_id) AS actors
GROUP BY uniqueId;
Some have suggested to use GROUP_CONCAT
to get it done but still I'm not able to achieve the expected format.
Any help or pointers are welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用mySQL 聚合函数:
尝试在这里。
You can use MySQL
JSON_ARRAYAGG
aggregation function:Try it here.