简体   繁体   English

将查询结果转换为postgresql中的一个数组json对象

[英]Convert query result to be one array json object in postgresql

My problem is that I want to convert the query result to be one json object in Postgresql我的问题是我想将查询结果转换为 Postgresql 中的一个 json 对象

For example, I have a query like this:例如,我有一个这样的查询:

SELECT row_to_json(r)
FROM 
(
    select ff.*, json_agg(ffo."option") as options
    from form_field ff, form_field_options ffo 
    where ffo.form_field_id = ff.id and ff.form_id = 'fef5c7e0-170c-4556-80d2-42e3db66cfa2'
    group by ff.id
) r

And the result is:结果是: 在此处输入图片说明

Is there anyway to convert the result to be something like: [{...}, {...}] so I can use it as a sub query in another query无论如何将结果转换为类似: [{...}, {...}] 这样我就可以将它用作另一个查询中的子查询

Thanks for reading <3感谢阅读 <3

you can add another layer of json_agg to convert it to a list of json objects.您可以添加另一层 json_agg 将其转换为 json 对象列表。

eg例如

SELECT json_agg(row_to_json(r)) my_json_data
FROM 
(
    select ff.*, json_agg(ffo."option") as options
    from form_field ff, form_field_options ffo 
    where ffo.form_field_id = ff.id and ff.form_id = 'fef5c7e0-170c-4556-80d2-42e3db66cfa2'
    group by ff.id
) r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM