From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Cc: Snjezana Frketic <frketic.snjezana@gmail.com>
Subject: Re: How to select values in a JSON type of column?
Date: Wed, 18 Nov 2020 18:46:40 +0100
Message-ID: <ff765692-7c0c-c893-d033-37f4d33412fe@gmx.net> (raw)
In-Reply-To: <CA+9_ahZJRqHVa11i6C7SEbbbp0YF6j2BDhvhFkWXrekBhsyvWQ@mail.gmail.com>
References: <CA+9_ahaS5YkqQ5B8=S6KRgSaspk0uedT5gBP0NS_YU2ym9O68g@mail.gmail.com>
<35c562d1-5324-6f10-f2a4-624da3acbbd2@gmx.net>
<CA+9_ahZJRqHVa11i6C7SEbbbp0YF6j2BDhvhFkWXrekBhsyvWQ@mail.gmail.com>
Snjezana Frketic schrieb am 18.11.2020 um 17:00:
> I actually have version 9.3.17 😬
>
>
> On Wed, 18 Nov 2020 at 15:55, Thomas Kellerer <shammat@gmx.net <mailto:shammat@gmx.net>> wrote:
>
> Snjezana Frketic schrieb am 18.11.2020 um 11:29:
> > I have a column called |targeting| in a table called |campaigns| .
> > [...]
> > and I need to select all the |ids| in |includes|.
> > Currently, I am doing it like this
> >
> > SELECT |targeting#>'{targets,0,audienceSegments,0,includes,0,segments,allOf,0,ids}'FROM campaigns;|
> >
>
> If you are on Postgres 12 or later, this can be done using jsonb_path_query_array:
>
> select jsonb_path_query_array(targeting, '$.targets[*].*.includes.**.ids.id <http://ids.id>';)
> from campaigns
If you are limited to an unsupported version, you need to go down the hierarchy manually:
select t.ids
from campaigns c
cross join lateral (
select array_agg(s2.seg2 ->> 'id') as ids
from json_array_elements(c.targeting -> 'targets') as t(target)
cross join json_array_elements(t.target -> 'audienceSegments') as a(aud)
cross join json_array_elements(a.aud -> 'includes') as i(include)
cross join json_array_elements(i.include #> '{segments,allOf}') as s(seg)
cross join json_array_elements(s.seg -> 'ids') as s2(seg2)
) t
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: shammat@gmx.net, frketic.snjezana@gmail.com
Subject: Re: How to select values in a JSON type of column?
In-Reply-To: <ff765692-7c0c-c893-d033-37f4d33412fe@gmx.net>
References: <CA+9_ahaS5YkqQ5B8=S6KRgSaspk0uedT5gBP0NS_YU2ym9O68g@mail.gmail.com> <35c562d1-5324-6f10-f2a4-624da3acbbd2@gmx.net> <CA+9_ahZJRqHVa11i6C7SEbbbp0YF6j2BDhvhFkWXrekBhsyvWQ@mail.gmail.com> <ff765692-7c0c-c893-d033-37f4d33412fe@gmx.net>
* 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