简体   繁体   English

如何为 json_build_object 添加默认值 postgresql

[英]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.

相关问题 PostgreSQL json_build_object 嵌套 - PostgreSQL json_build_object nested PostgreSQL Json_build_object / json_agg 添加聚合级别 - PostgreSQL Json_build_object / json_agg add level of aggregation 如何将多个 JSON_BUILD_OBJECT 条目添加到 JSON_AGG - How to Add Multiple JSON_BUILD_OBJECT entries to a JSON_AGG 在PostgreSQL中使用json_build_object和json_build_array排序json结构 - Ordering json structure using json_build_object and json_build_array in Postgresql 使用 json_agg(json_build_object(.......)) 时,如何确保 json_build_object 返回空列表 - when use json_agg(json_build_object(.......)) ,how to ensure json_build_object return empty list 如何在 Postgres 的 json_build_object() 中进行 GROUP BY 和 COUNT - How to do a GROUP BY and COUNT within json_build_object() in Postgres 无法使用 json_build_object 从 postgreSQL 查询创建大型 json 对象 - Unable to create a large json object from a postgreSQL query using json_build_object json_build_object()中的大小写表达式 - Case expression in json_build_object() 在 PostgreSQL v14.x 中使用 json_build_object 和 SELECT 语句的结果 - Using json_build_object in PostgreSQL v14.x with the result of a SELECT statement SQL,在使用 json_agg 和 json_build_object 时遇到一些问题 - SQL, having some trouble with json_agg & json_build_object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM