pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to select values in a JSON type of column?
9+ messages / 3 participants
[nested] [flat]
* How to select values in a JSON type of column?
@ 2020-11-18 10:29 Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:40 ` Re: How to select values in a JSON type of column? David G. Johnston <david.g.johnston@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
0 siblings, 2 replies; 9+ messages in thread
From: Snjezana Frketic @ 2020-11-18 10:29 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi!
I have a column called targeting in a table called campaigns .
The column looks like
{
"targets": [
{
"audienceSegments": [
{
"includes": [
{
"consumer": "selection",
"segments": {
"allOf": [
{
"provider": "a",
"ids": [
{
"id": "110418"
},
{
"id": "110430"
},
{
"id": "110433"
}
]
}
]
}
}
],
"excludes": [
{
"consumer": "selection",
"segments": {
"allOf": [
{
"provider": "a",
"ids": [
{
"id": "109776"
}
]
}
]
}
}
]
}
]
}
]
}
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;
and that works, but, I don’t want to have a fixed path because positions
could change like 0 could become 1, includes and excludes could change
positions, allOf could be anyOf etc.
Any idea of how to always select ids in includes no matter the changes?
Thank you!
Anna
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
@ 2020-11-18 14:40 ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-18 14:48 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
1 sibling, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2020-11-18 14:40 UTC (permalink / raw)
To: Snjezana Frketic <frketic.snjezana@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wednesday, November 18, 2020, Snjezana Frketic <
frketic.snjezana@gmail.com> wrote:
>
>
> SELECT targeting#>'{targets,0,audienceSegments,0,includes,0,
> segments,allOf,0,ids}'FROM campaigns;
>
> and that works, but, I don’t want to have a fixed path because positions
> could change like 0 could become 1, includes and excludes could change
> positions, allOf could be anyOf etc.
> Any idea of how to always select ids in includes no matter the changes?
>
Maybe it can be done using json path:
https://www.postgresql.org/docs/13/functions-json.html#FUNCTIONS-SQLJSON-PATH
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:40 ` Re: How to select values in a JSON type of column? David G. Johnston <david.g.johnston@gmail.com>
@ 2020-11-18 14:48 ` Snjezana Frketic <frketic.snjezana@gmail.com>
0 siblings, 0 replies; 9+ messages in thread
From: Snjezana Frketic @ 2020-11-18 14:48 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
I looked at it yesterday, but I couldn't figure it out because my JSON is
more nested and I got lost going down the path.
On Wed, 18 Nov 2020 at 15:40, David G. Johnston <david.g.johnston@gmail.com>
wrote:
>
> On Wednesday, November 18, 2020, Snjezana Frketic <
> frketic.snjezana@gmail.com> wrote:
>
>>
>>
>> SELECT
>> targeting#>'{targets,0,audienceSegments,0,includes,0,segments,allOf,0,ids}'FROM
>> campaigns;
>>
>> and that works, but, I don’t want to have a fixed path because positions
>> could change like 0 could become 1, includes and excludes could change
>> positions, allOf could be anyOf etc.
>> Any idea of how to always select ids in includes no matter the changes?
>>
>
> Maybe it can be done using json path:
>
>
> https://www.postgresql.org/docs/13/functions-json.html#FUNCTIONS-SQLJSON-PATH
>
> David J.
>
>
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
@ 2020-11-18 14:55 ` Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
1 sibling, 1 reply; 9+ messages in thread
From: Thomas Kellerer @ 2020-11-18 14:55 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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')
from campaigns
Online example: https://dbfiddle.uk/?rdbms=postgres_12&fiddle=ee7f6e73055ffb3a98fcfd3d86763e35
Thomas
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
@ 2020-11-18 16:00 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:21 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 17:46 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
0 siblings, 2 replies; 9+ messages in thread
From: Snjezana Frketic @ 2020-11-18 16:00 UTC (permalink / raw)
To: Thomas Kellerer <shammat@gmx.net>; +Cc: pgsql-general@lists.postgresql.org
I actually have version 9.3.17 😬
On Wed, 18 Nov 2020 at 15:55, Thomas Kellerer <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')
> from campaigns
>
>
> Online example:
> https://dbfiddle.uk/?rdbms=postgres_12&fiddle=ee7f6e73055ffb3a98fcfd3d86763e35
>
> Thomas
>
>
>
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
@ 2020-11-18 16:21 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:30 ` Re: How to select values in a JSON type of column? David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 1 reply; 9+ messages in thread
From: Snjezana Frketic @ 2020-11-18 16:21 UTC (permalink / raw)
To: Thomas Kellerer <shammat@gmx.net>; +Cc: pgsql-general@lists.postgresql.org
Unfortunately, I also can not update my version :)
On Wed, 18 Nov 2020 at 17:00, Snjezana Frketic <frketic.snjezana@gmail.com>
wrote:
> I actually have version 9.3.17 😬
>
>
> On Wed, 18 Nov 2020 at 15:55, Thomas Kellerer <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')
>> from campaigns
>>
>>
>> Online example:
>> https://dbfiddle.uk/?rdbms=postgres_12&fiddle=ee7f6e73055ffb3a98fcfd3d86763e35
>>
>> Thomas
>>
>>
>>
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:21 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
@ 2020-11-18 16:30 ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-18 16:56 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2020-11-18 16:30 UTC (permalink / raw)
To: Snjezana Frketic <frketic.snjezana@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, Nov 18, 2020 at 9:23 AM Snjezana Frketic <frketic.snjezana@gmail.com>
wrote:
> Unfortunately, I also can not update my version :)
>
Then probably the answer to your original question is no :)
There are possibly other ways to make something that works but if you
aren't willing to upgrade off of a discontinued version, onto one which has
a perfectly usable solution, then my interest in pondering a work-around is
near zero.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:21 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:30 ` Re: How to select values in a JSON type of column? David G. Johnston <david.g.johnston@gmail.com>
@ 2020-11-18 16:56 ` Snjezana Frketic <frketic.snjezana@gmail.com>
0 siblings, 0 replies; 9+ messages in thread
From: Snjezana Frketic @ 2020-11-18 16:56 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Fair point.
Appreciate your help nevertheless :)
On Wed, 18 Nov 2020 at 17:30, David G. Johnston <david.g.johnston@gmail.com>
wrote:
> On Wed, Nov 18, 2020 at 9:23 AM Snjezana Frketic <
> frketic.snjezana@gmail.com> wrote:
>
>> Unfortunately, I also can not update my version :)
>>
>
> Then probably the answer to your original question is no :)
>
> There are possibly other ways to make something that works but if you
> aren't willing to upgrade off of a discontinued version, onto one which has
> a perfectly usable solution, then my interest in pondering a work-around is
> near zero.
>
> David J.
>
>
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: How to select values in a JSON type of column?
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Re: How to select values in a JSON type of column? Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Re: How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
@ 2020-11-18 17:46 ` Thomas Kellerer <shammat@gmx.net>
1 sibling, 0 replies; 9+ messages in thread
From: Thomas Kellerer @ 2020-11-18 17:46 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org; +Cc: Snjezana Frketic <frketic.snjezana@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
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2020-11-18 17:46 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-18 10:29 How to select values in a JSON type of column? Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:40 ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-18 14:48 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 14:55 ` Thomas Kellerer <shammat@gmx.net>
2020-11-18 16:00 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:21 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 16:30 ` David G. Johnston <david.g.johnston@gmail.com>
2020-11-18 16:56 ` Snjezana Frketic <frketic.snjezana@gmail.com>
2020-11-18 17:46 ` Thomas Kellerer <shammat@gmx.net>
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