pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Extracting data from jsonb array?
Date: Mon, 7 Dec 2020 18:13:06 -0700
Message-ID: <72f54672-ad66-0370-9128-206e49389b60@gmail.com> (raw)
In-Reply-To: <CAD3a31WNrMJq5bxQZ2+z5pEQU=wAn7Gtx2ahSmt6w5LiLqs57A@mail.gmail.com>
References: <CAD3a31UQuYmXLuHLFtF1NTZyFSjdHfAKeSkie8GT5K_2ahAh=Q@mail.gmail.com>
	<CAKE1AiaA7f_KJL0Qre=2Cnev4geGsrGmdbFPQYLpfCZnMS=DkQ@mail.gmail.com>
	<CAKFQuwaxB6bK_CSJaWM=OXjXya7BaV+3Pwpp9e4dPPSfB0CSrQ@mail.gmail.com>
	<CAD3a31VDRAV8_snO9SbxwJa77=aUJJTcsSvCRnc1v5mG8NqDkA@mail.gmail.com>
	<CAKFQuwY710pBKLXDhaGD-Sg27FOZro5uZ7GvBO8=aHbUYYWyWg@mail.gmail.com>
	<CAD3a31WNrMJq5bxQZ2+z5pEQU=wAn7Gtx2ahSmt6w5LiLqs57A@mail.gmail.com>



> OK, let me try asking again.  (I'm trying to actually get something 
> that works.)  So given an example like this:
>
> CREATE TEMP TABLE foo (
>   id INTEGER,
>   js  JSONB
> );
>
> INSERT INTO foo (id,js) VALUES (1,
> '[
> {"key":"r1kval","key2":"r1k2val"},
> {"key":"r1kval2","key2":"r1k2val2"}
> ]');
> INSERT INTO foo (id,js) VALUES (2,
> '[
> {"key":"r2kval","key2":"r2k2val"},
> {"key":"r2kval2","key2":"r2k2val2"}
> ]');
>
> Can anyone help me with a working query (preferably with the least 
> cumbersome syntax possible!) that would return these values (the key2 
> values) as array text elements:
>
> id     Agg_val
> ----  ------------------------
> 1     {r1k2val,r1k2val2}
> 2     {r2k2val,r2k2val}
>
> (2 rows)


postgres=# select id, array_agg(fa) from (select id, 
(jsonb_array_elements(js)->'key') as fa from foo) g group by id order by id;
  id |          array_agg
----+------------------------------
   1 | {"\"r1kval\"","\"r1kval2\""}
   2 | {"\"r2kval\"","\"r2kval2\""}
(2 rows)

I think the quotes are a fault of example data?

Cumbersome is in the eyes of the beholder ;)

view thread (25+ messages)  latest in thread

Message-ID: <72f54672-ad66-0370-9128-206e49389b60@gmail.com>
Permalink:  ../72f54672-ad66-0370-9128-206e49389b60@gmail.com/
Also on:    postgresql.org/message-id/72f54672-ad66-0370-9128-206e49389b60@gmail.com

 · 

reply

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: robjsargent@gmail.com
  Subject: Re: Extracting data from jsonb array?
  In-Reply-To: <72f54672-ad66-0370-9128-206e49389b60@gmail.com>
  References: <CAD3a31UQuYmXLuHLFtF1NTZyFSjdHfAKeSkie8GT5K_2ahAh=Q@mail.gmail.com> <CAKE1AiaA7f_KJL0Qre=2Cnev4geGsrGmdbFPQYLpfCZnMS=DkQ@mail.gmail.com> <CAKFQuwaxB6bK_CSJaWM=OXjXya7BaV+3Pwpp9e4dPPSfB0CSrQ@mail.gmail.com> <CAD3a31VDRAV8_snO9SbxwJa77=aUJJTcsSvCRnc1v5mG8NqDkA@mail.gmail.com> <CAKFQuwY710pBKLXDhaGD-Sg27FOZro5uZ7GvBO8=aHbUYYWyWg@mail.gmail.com> <CAD3a31WNrMJq5bxQZ2+z5pEQU=wAn7Gtx2ahSmt6w5LiLqs57A@mail.gmail.com> <72f54672-ad66-0370-9128-206e49389b60@gmail.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