From: Joel Jacobson <joel@compiler.org>
To: pgsql-general@postgresql.org
Subject: Avoid undesired flattening of jsonb arrays?
Date: Fri, 18 Dec 2020 16:23:40 +0100
Message-ID: <0d72b76d-ca2b-4263-8888-d6dfca861c51@www.fastmail.com> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: joel@compiler.org
Subject: Re: Avoid undesired flattening of jsonb arrays?
In-Reply-To: <0d72b76d-ca2b-4263-8888-d6dfca861c51@www.fastmail.com>
References: <0d72b76d-ca2b-4263-8888-d6dfca861c51@www.fastmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox