Re: generating json without nulls

From: Tim Dudgeon <tdudgeon(dot)ml(at)gmail(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: generating json without nulls
Date: 2015-05-19 09:48:40
Message-ID: 555B06F8.3020000@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thanks.
Using the CASE ... THEN ... ELSE ... END approach seems to work best for me.
Not especially elegant, but it does the job.

Tim

On 07/05/2015 17:27, David G. Johnston wrote:
> On Thu, May 7, 2015 at 8:29 AM, Tim Dudgeon <tdudgeon(dot)ml(at)gmail(dot)com
> <mailto:tdudgeon(dot)ml(at)gmail(dot)com>>wrote:
>
> That's not going to work. I want the row, I just don't want the
> values that are null.
>
>
> Only thing that comes to mind:
> 1. Use the conversion function to get the json structure with nulls.
> 2. Use an explode function to convert the json into a table structure
> with (key, value) columns.
> 3. Filter that table where value is not null.
> 4. Convert the remaining entries into arrays
> 5. Pass the two arrays back into the json_object(keys text[], values
> text[])
>
> You could dynamically build up a literal string array but the syntax
> challenges scare me:
> json_object('{' ||
> CASE WHEN col1 IS NULL THEN '' ELSE '"col1",' || val1 || '"' END ||
> CASE WHEN col2 IS NULL THEN '' ELSE '"col2",' || val2 || '"' END ||
> '}'::text[])
>
> David J.
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ravi Krishna 2015-05-19 23:47:12 Does PG support bulk operation in embedded C
Previous Message ktm@rice.edu 2015-05-12 17:02:01 Re: ERROR: column "gid" specified more than once