[英]How to add a default value to json_build_object postgresql
My json looks like this:-我的 json 看起来像这样:-
{"DestinationLists": [{"name": "TVNZ, Mediaworks, Choice", "destinations": []}, { "name": "TVNZ, Discovery", "destinations": [165, 183, 4155]}]}
My desired output is one row for each object in the array like:-我想要的 output 是数组中每个 object 的一行,例如:-
{"name" : "TVNZ, Mediaworks, Choice", "destinations" : []}
{"name" : "TVNZ, Discovery", "destinations" : [{"Id" : 165}, {"Id" : 183}, {"Id" : 4155}]}
My query is this:-我的查询是这样的:-
WITH ExpandedData AS (
SELECT
"Id",
setting,
json_build_object(
'name',
dl->>'name',
'destinations',
json_agg(
json_build_object('Id',dld::text::int)
)
) as DestinationListItem
FROM
feature.settings,
jsonb_array_elements(value->'DestinationLists') dl,
jsonb_array_elements(dl->'destinations') dld
GROUP BY
"Id",dl->>'name'
)
select * from ExpandedData
But this query is missing destinations
where the array is empty so the result I am getting from the query is:-但是这个查询缺少数组为空的destinations
,所以我从查询中得到的结果是:-
{"name" : "TVNZ, Discovery", "destinations" : [{"Id" : 165}, {"Id" : 183}, {"Id" : 4155}]}
just one row instead of two.只有一排而不是两排。 How do I update this query to get the desired outcome?如何更新此查询以获得所需的结果?
DBFiddle here:- https://www.db-fiddle.com/f/tDA7SajDoWhXrQaAa1j2qN/0 DBFiddle 在这里:- https://www.db-fiddle.com/f/tDA7SajDoWhXrQaAa1j2qN/0
EDIT: I have been able to further drill down my problem, so the issue is basically happening from this line of the query:-编辑:我已经能够进一步深入研究我的问题,所以问题基本上是从这行查询中发生的:-
jsonb_array_elements(dl->'destinations') dld
Basically when the destinations
array is empty then jsonb_array_elements
does not return anything so I guess the right solution here would be to somehow return an empty array from jsonb_array_elements
when an empty array is passed to it.基本上,当destinations
数组为空时, jsonb_array_elements
不会返回任何内容,所以我想这里正确的解决方案是在将空数组传递给它时以某种方式从jsonb_array_elements
返回一个空数组。
You can use a recursive cte
:您可以使用递归cte
:
with recursive cte(js, ind, r) as (
select js, 1, case when json_array_length(js -> 'DestinationLists' -> 0 -> 'destinations') = 0 then (js -> 'DestinationLists' -> 0)::jsonb else jsonb_set(js::jsonb, concat('{DestinationLists,', 0, ',destinations}')::text[], (select array_to_json(array_agg(json_build_object('Id', value))) from json_array_elements(js -> 'DestinationLists' -> 0 -> 'destinations'))::jsonb) end from data
union all
select js, ind+1, case when json_array_length(js -> 'DestinationLists' -> ind -> 'destinations') = 0 then (js -> 'DestinationLists' -> ind)::jsonb else (jsonb_set(js::jsonb, concat('{DestinationLists,', ind, ',destinations}')::text[], (select array_to_json(array_agg(json_build_object('Id', value))) from json_array_elements(js -> 'DestinationLists' -> ind -> 'destinations'))::jsonb) -> 'DestinationLists' -> ind)::jsonb end from cte where ind < json_array_length(js -> 'DestinationLists')
)
select r from cte;
Output: Output:
r r |
---|
{"name": "TVNZ, Mediaworks, Choice", "destinations": []} {"name": "TVNZ, Mediaworks, Choice", "destinations": []} |
{"name": "TVNZ, Discovery", "destinations": [{"Id": 165}, {"Id": 183}, {"Id": 4155}]} {"name": "TVNZ, Discovery", "destinations": [{"Id": 165}, {"Id": 183}, {"Id": 4155}]} |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.