Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yue8n-00017u-Ah for pgsql-sql@arkaria.postgresql.org; Tue, 19 May 2015 09:48:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Yue8m-00007U-I4 for pgsql-sql@arkaria.postgresql.org; Tue, 19 May 2015 09:48:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yue8l-00007O-GE for pgsql-sql@postgresql.org; Tue, 19 May 2015 09:48:47 +0000 Received: from mail-wi0-x22a.google.com ([2a00:1450:400c:c05::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Yue8i-0004w2-T2 for pgsql-sql@postgresql.org; Tue, 19 May 2015 09:48:46 +0000 Received: by wizk4 with SMTP id k4so110366391wiz.1 for ; Tue, 19 May 2015 02:48:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=kHIPxfbM0aiaGfOkDUIRPmGh4p/8t8PzXQfPW6x88Z0=; b=wUqx1Zz+HtQzy4i0ZcT9aBEmAaNZ4EXd6VgvZYGaRa6ty7Qvjn8qp7mjK+uQqsQKZe ndLpAmUA7tNfj1+ys5XE8hl1eEXN4OZv9VbTy7G9tvHYH2Boe0bVvNRZ6iSKMxjS3p7h 4jgmWbiDgOwDUHBF4vYJb/0fcGzPX5jOWztEY6L/BEUGLwEXhO1w3g/d6JiPEWRXFS5t M3uW1YUiU3Ng8jC2P1IysLxYcW1CTYSy+bhBrPEUVcBkT+vigucfHhJxHf3o3/Pce+Kz rhk/9zJAn4nuTCpr+iejxu2X8gTrEYzrhaKnNmfwiYP0PmqTiW8dtkTh8CQJvuFfjK70 hGFA== X-Received: by 10.180.7.134 with SMTP id j6mr10067462wia.9.1432028923220; Tue, 19 May 2015 02:48:43 -0700 (PDT) Received: from timbomac.home (host86-147-75-164.range86-147.btcentralplus.com. [86.147.75.164]) by mx.google.com with ESMTPSA id q10sm9872050wjo.38.2015.05.19.02.48.41 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 May 2015 02:48:42 -0700 (PDT) Message-ID: <555B06F8.3020000@gmail.com> Date: Tue, 19 May 2015 10:48:40 +0100 From: Tim Dudgeon User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: "pgsql-sql@postgresql.org" Subject: Re: generating json without nulls References: <554B84C0.1070007@gmail.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------080201090302040500040401" X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This is a multi-part message in MIME format. --------------080201090302040500040401 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 >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. > --------------080201090302040500040401 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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.ml@gmail.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.


--------------080201090302040500040401--