create table your_table as select
'Canada' as country_name,
'CA' as country_code,
'CAD' as currency_code;
select jsonb_build_object(
'country_name', country_name,
'country_code', country_code,
'currency_code', currency_code
)
from your_table;
jsonb_build_object |
---|
{"country_code": "CA", "country_name": "Canada", "currency_code": "CAD"} |
请注意,这是在标题中,而不是实际字段中。要去掉它,您需要在您的客户端/应用程序中去除它。如果您更新问题以指定您使用的客户端/库+语言,并显示您如何获取这些记录,将会很有帮助。
result=result['jsonb_build_object']
将会剥离掉顶层。演示。请记得在添加更多细节时编辑问题 - 如果你把额外信息放在单独的评论中,其他人会更难跟踪。 - undefinedWITH cte AS (
SELECT jsonb_build_object(
'country_name', country_name,
'country_code', country_code,
'currency_code', currency_code
) AS jsonb_data
FROM table
)
SELECT jsonb_data
FROM cte;
with temp_data as (
select jsonb_build_object(
'country_name', country_name,
'country_code', country_code,
'currency_code', currency_code
)
from table
)
select t->'jsonb_build_object' from temp_data t