Re: Bypassing NULL elements in row_to_json function

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Michael Nolan <htfoot(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Bypassing NULL elements in row_to_json function
Date: 2016-04-08 15:53:46
Message-ID: 5707D40A.3080102@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 08/04/2016 16:31, Michael Nolan wrote:
> I'm looking at the possibility of using JSON as a data exchange format
> with some apps running on both PCs and Macs. .
>
> The table I would be exporting has a lot of NULL values in it. Is
> there any way to skip the NULL values in the row_to_json function and
> include only the fields that are non-null?

You could use a CTE to filter out the nulls (not tested - I haven't used
JSON in PG (yet!)):

with no_nulls as (
select ... from my_table
where whatever is not null
)
select row_to_json(....) from no_nulls;

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod(at)iol(dot)ie

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bannert Matthias 2016-04-08 16:31:46 Re: max_stack_depth problem though query is substantially smaller
Previous Message Scott Ribe 2016-04-08 15:44:16 recover from this error