Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
Дата
Msg-id CAKFQuwb+d5sfx2ALRcM-H3N=aK4wH32j42BdJJpcOuOszEo4Cw@mail.gmail.com
обсуждение исходный текст
Ответ на coalesce(json_agg [..] filter where [..], '[]' in left join returning null  (Stefan Houtzager <stefan.houtzager@gmail.com>)
Ответы Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null  (Stefan Houtzager <stefan.houtzager@gmail.com>)
Список pgsql-novice
On Wed, Jan 6, 2021 at 3:11 AM Stefan Houtzager <stefan.houtzager@gmail.com> wrote:

                COALESCE(

                json_agg(
                       (select row_to_json(_) from
                           (select vp.percentage, vp.version, json_build_object(
                               'lower', lower(vp.validity),
                               'upper', upper(vp.validity),
                               'lower_inc', lower_inc(vp.validity),
                               'upper_inc', upper_inc(vp.validity)
                           ) validity)
                        as _)
                ) FILTER (WHERE vp.vatcat_id IS NOT NULL), '[]'::JSON) vat_percentage
{ "vat_cat": { "id": 10, "descr": "nonsense", "expense": true, "version": 1, "vat_percentage": null } } 

How do I get the query right so that it display [] instead of null?


Use COALESCE(NULLIF(..., 'null'::json), '[]'::json); NULLIF converts the JSON null into SQL NULL which the COALESCE then replaces with a empty json array.

David J.

В списке pgsql-novice по дате отправления:

Предыдущее
От: Pól Ua Laoínecháin
Дата:
Сообщение: Re: Best practice: call an internal postgresql function (e.g. raw_parser) from another C/Rust binary
Следующее
От: Stefan Houtzager
Дата:
Сообщение: Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null