append jsonb array to another jsonb array

Started by Joao Ferreira gmailabout 5 years ago4 messagesgeneral
Jump to latest
#1Joao Ferreira gmail
joao.miguel.c.ferreira@gmail.com

Hello all,

I PL/pgSQL I have a few jsonb variables (instantiated each with an array of
objects) and I need to append them to produce the final result. All of them
are small in size (max 30 each). We are using Pg 11.

Example:
a = [ x, y ]
b = [ z , w ]
result would be [ x, y, z, w ]

What would you consider to be a suitable approach ?

thanks
João

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Joao Ferreira gmail (#1)
Re: append jsonb array to another jsonb array

On Wednesday, February 17, 2021, Joao Miguel Ferreira <
joao.miguel.c.ferreira@gmail.com> wrote:

We are using Pg 11.

Example:
a = [ x, y ]
b = [ z , w ]
result would be [ x, y, z, w ]

What would you consider to be a suitable approach ?

That’s called concatenation.

https://www.postgresql.org/docs/11/functions-json.html

David J.

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Joao Ferreira gmail (#1)
Re: append jsonb array to another jsonb array

Hi

st 17. 2. 2021 v 20:44 odesílatel Joao Miguel Ferreira <
joao.miguel.c.ferreira@gmail.com> napsal:

Hello all,

I PL/pgSQL I have a few jsonb variables (instantiated each with an array
of objects) and I need to append them to produce the final result. All of
them are small in size (max 30 each). We are using Pg 11.

Example:
a = [ x, y ]
b = [ z , w ]
result would be [ x, y, z, w ]

What would you consider to be a suitable approach ?

postgres=# select '[10,20,30]'::jsonb || '[100,20]'::jsonb;
┌───────────────────────┐
│ ?column? │
╞═══════════════════════╡
│ [10, 20, 30, 100, 20] │
└───────────────────────┘
(1 row)

Regards

Pavel

Show quoted text

thanks
João

#4Joao Ferreira gmail
joao.miguel.c.ferreira@gmail.com
In reply to: Pavel Stehule (#3)
Re: append jsonb array to another jsonb array

Hi, David, Pavel,

Thank you for your indications

Joao

On Wed, Feb 17, 2021 at 7:50 PM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Show quoted text

Hi

st 17. 2. 2021 v 20:44 odesílatel Joao Miguel Ferreira <
joao.miguel.c.ferreira@gmail.com> napsal:

Hello all,

I PL/pgSQL I have a few jsonb variables (instantiated each with an array
of objects) and I need to append them to produce the final result. All of
them are small in size (max 30 each). We are using Pg 11.

Example:
a = [ x, y ]
b = [ z , w ]
result would be [ x, y, z, w ]

What would you consider to be a suitable approach ?

postgres=# select '[10,20,30]'::jsonb || '[100,20]'::jsonb;
┌───────────────────────┐
│ ?column? │
╞═══════════════════════╡
│ [10, 20, 30, 100, 20] │
└───────────────────────┘
(1 row)

Regards

Pavel

thanks
João