Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqHc8-0001oN-NN for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 15:24:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqHc6-0003De-Bm for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 15:24:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqHc5-0003DX-3Q for pgsql-general@lists.postgresql.org; Fri, 18 Dec 2020 15:24:14 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqHbx-0000aL-Ey for pgsql-general@postgresql.org; Fri, 18 Dec 2020 15:24:12 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 1BDA4970 for ; Fri, 18 Dec 2020 10:24:02 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Fri, 18 Dec 2020 10:24:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:date:from:to:subject:content-type; s= fm3; bh=aOr5HxO2NVOQUWd8SbA5Ekb1gtciBRGdiRQe/6aiQUY=; b=DMd1PKJX y/QtfGxKvsuEodtgGgUeFrU8d8NrQ/WuO6Aj45WIJOquZYIC9o+FZqGPUy8eBXP8 dNFUuk4IyypSoi0WXzFlmGqLmJ2Ekfvvu6CtloWM7Xo1OutqE4JeRfInsmKOy7pI er1DOtDfanOcjTRGOelPobw5eleiHJ68u+dK/9+ZGrCE+Z9cY3m4C4CxG6/F5FXQ FxX1iCPDbKPo6qN7YFOcNwDfUM747clPNFL9yCA2TYoyFsXG37B330EcIhDDpbek qkgFXg5upkYxhCaKsE4rnPWRFHUPDi6KqQo9AzYJx7Cq5cQ6TD7blLO9xlmH9xU3 KQOiEZwHgvDv2A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=aOr5HxO2NVOQUWd8SbA5Ekb1gtciB RGdiRQe/6aiQUY=; b=U8fg5duTMyyknKcyO4RWGuj3kUAE//VTwRPM+CLagsQRz OBWFCawfdMFjpc7WbaTbSW2CDTRLG4+OZwg5iC7+SL9JgoCcw7xCKvUsMxlBrmEi mz+WWe4debhJWbwadNUuznFDuUMCtrpmsAcJkdCWSiS2PXhcAZz4yYcbQlJLt7zq OpoyS5oybjZ/94nIGPZQTRPzToHDdTKCumnG4zbW6bXMiVdUvEJgEc9DLzkWPAik lvH6j3yn2CHX5kdixMrnf2myqKpX/uM57CYcrPc3ogdYGMLi9Hc6DHM7S6rrUAr3 Fsp7n15fTPYCwsPGECbbILXBmoMgqanLvkahGNvnA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudeliedgjeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnhepgefgvdfhgfefjedtleegteejje fhieegjedvhfehueetfeektdeiieefheffkeelnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id E60F8E0500; Fri, 18 Dec 2020 10:23:58 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-Id: <0d72b76d-ca2b-4263-8888-d6dfca861c51@www.fastmail.com> Date: Fri, 18 Dec 2020 16:23:40 +0100 From: "Joel Jacobson" To: pgsql-general@postgresql.org Subject: Avoid undesired flattening of jsonb arrays? Content-Type: multipart/alternative; boundary=6caf56508c5246d08aea9f133fd5039a List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --6caf56508c5246d08aea9f133fd5039a Content-Type: text/plain The || operator for the jsonb type has a surprising behaviour. Instead of appending the right operand "as is" to the left operand, it has a magic behaviour if the right operand is an array, in which case it will append the items of the array, instead of appending the array itself as a single value. Example: SELECT '[10,20]'::jsonb || '30'::jsonb; [10, 20, 30] SELECT '[10,20]'::jsonb || '[30]'::jsonb; [10, 20, 30] Since [10, 20, [30]] is desired in our case, we must use jsonb_insert() to work-around the problem in a not very nice way: SELECT jsonb_insert('[10,20]'::jsonb,'{-1}','[30]'::jsonb,TRUE); [10, 20, [30]] Suggestions welcome if there is a better way to solve this problem. Best regards, Joel --6caf56508c5246d08aea9f133fd5039a Content-Type: text/html Content-Transfer-Encoding: quoted-printable
The || operator= for the jsonb type has a surprising behaviour.

=
Instead of appending the right operand "as is" to the left operand,=
it has a magic behaviour if the right operand is an array= ,
in which case it will append the items of the array,
=
instead of appending the array itself as a single value.
<= /div>

Example:

SELECT '[= 10,20]'::jsonb || '30'::jsonb;
[10, 20, 30]
=
SELECT '[10,20]'::jsonb || '[30]'::jsonb;
[= 10, 20, 30]

Since [10, 20, [30]] is desired= in our case, we must use jsonb_insert() to work-around the problem in a= not very nice way:

SELECT jsonb_insert('[1= 0,20]'::jsonb,'{-1}','[30]'::jsonb,TRUE);
[10, 20, [30]]

Suggestions welcome if there is a better way= to solve this problem.

Best regards,

Joel
--6caf56508c5246d08aea9f133fd5039a--