Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aLaj3-00080X-G1 for pgsql-sql@arkaria.postgresql.org; Tue, 19 Jan 2016 18:09:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aLaj3-0003Jy-2V for pgsql-sql@arkaria.postgresql.org; Tue, 19 Jan 2016 18:09:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aLaj2-0003Js-O2 for pgsql-sql@postgresql.org; Tue, 19 Jan 2016 18:09:52 +0000 Received: from mail-yk0-x232.google.com ([2607:f8b0:4002:c07::232]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aLaiz-0003WQ-C3 for pgsql-sql@postgresql.org; Tue, 19 Jan 2016 18:09:52 +0000 Received: by mail-yk0-x232.google.com with SMTP id a85so579533320ykb.1 for ; Tue, 19 Jan 2016 10:09:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=0YDFQzA3VioxvI+R+Z1jnvWu/hbkXCCqpdfkcbyI/tg=; b=mc/1kDp9UJ6JolfVHj8xKYZ8BxbbYbwk/a2ZpEZqP39Bj59Wdk251EqDyAqGbKn8iM 4cd876gHwhJs041TyZiNdBj2FiWbui8zgctId52sQYAzjwxlWzXHCOfjpE8NbcNbvJ1h JlsMbTgFTGUdVVF6asN/0feDz9PTp3cGIBt7ow3B/PWM813J0bo1ASalf5IyBFGoelpD 7DHAhSkGDhlHsql2fGj/dktyVZg+LQFDY0fvXLpSoAFPGrE6ypFPGkAdaWxZvW/fI/Dl hw7hQpxQTHNLqJVkztuvusMOfWXLPDmmSgL9ufbr0xyrBS708fRFytGvNZF8dh3DDoQD vFxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=0YDFQzA3VioxvI+R+Z1jnvWu/hbkXCCqpdfkcbyI/tg=; b=AEwk8iHqJXXzQEmiKPe9XMHofcmQX7iSNCM15v8KQU7Dfin6CBIHUBAaiQOltelfx6 MQV2jPE9bTgEsS1Qaq9IyDfE+2iwXR0WyoZc1nq9cYyWIyB0tnx5W/Soaoq7y00SLU8I EeLVz4YIRXO8Wf0l+bZTNinpJfXxplLN5ua3l/1B3tLvIQh9cesBp/FBo+IP7ZJUW+Ot kVHEfbt+hepu0UzDPNQ4eyfbjOZAWEmGhWNk75AA1IZURrVtJd0KMjCxZchzugJe9yKB nvgDNGfo1MfdUEXu0uX35KgbWmTtN5zLVIFpbrQloE28sDdnCMTADqTa2dBUe2aqUoRo 8Jig== X-Gm-Message-State: ALoCoQkG5C6AmXs/suWb0YBX353tXxWag3XDivMiMbmiKHZ60c5TBZoaawyYh3SDH2a/iqoVwumCf9QxEWZajmYDvdd7/VZ11g== X-Received: by 10.37.84.6 with SMTP id i6mr8294748ybb.94.1453226985952; Tue, 19 Jan 2016 10:09:45 -0800 (PST) MIME-Version: 1.0 Received: by 10.129.85.202 with HTTP; Tue, 19 Jan 2016 10:09:06 -0800 (PST) In-Reply-To: References: From: Pavel Stehule Date: Tue, 19 Jan 2016 19:09:06 +0100 Message-ID: Subject: Re: remove null values from json To: Michael Moore Cc: postgres list Content-Type: multipart/alternative; boundary=001a11c17776469c9e0529b3c6c6 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 --001a11c17776469c9e0529b3c6c6 Content-Type: text/plain; charset=UTF-8 2016-01-15 21:24 GMT+01:00 Michael Moore : > This is ALMOST what I want: > select json_strip_nulls ('[{ "f1":1 , "f2":null , "f3":"NULL" , "f4":"" > }]'); > the result is: > "[{"f1":1,"f3":"NULL","f4":""}]" > > As you can see, the "f2" object has been removed. I would *also* like to > remove the f3 and f4 objects. > > I realize I won't be able to use the json_strip_nulls function to do this. > I could brute force it by doing "json to array" then looping through the > array and picking only the objects I want, and then using the chosen > objects to build an new JSON document. > > I suspect there is a more elegant way to accomplish the same objective. > Ideas? > if you generate JSON from zero, then replace "" and "NULL" by NULL before jsonification. Pavel > > PS. It would be great to have a json_strip_values (json, array) where > array contains the list of values you want removed. > --001a11c17776469c9e0529b3c6c6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


2016-01-15 21:24 GMT+01:00 Michael Moore <michaeljmoore@gmail.c= om>:
This = is ALMOST what I want:
select json_s= trip_nulls ('[{ "f1":1 , "f2":null =C2=A0, "f3= ":"NULL" , "f4":"" =C2=A0}]');
the result is:
&quo= t;[{"f1":1,"f3":"NULL","f4":"&= quot;}]"

As you can see, the "= ;f2" object has been removed. I would also like to remove the f= 3 and f4 objects.=C2=A0

I realize I won't be a= ble to use the json_strip_nulls function to do this. I could brute force it= by doing "json to array" then looping through the array and pick= ing only the objects I want, and then using the chosen objects to build an = new JSON document.=C2=A0

I suspect there is a more= elegant way to accomplish the same objective.
Ideas?
=

if you generate JSON from zero, then repla= ce "" and "NULL" by NULL before jsonification.

<= /div>
Pavel
=C2=A0

PS. It would be great to have a=C2=A0json_strip_values (json, array) wher= e array contains the list of values you want removed.=C2=A0

--001a11c17776469c9e0529b3c6c6--