From: Alban Hertroys <haramrae@gmail.com>
To: Shaheed Haque <shaheedhaque@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Using JSONB with nested key-value structures, and skipping/wildcarding the key
Date: Tue, 2 Jun 2020 00:50:20 +0200
Message-ID: <960EE185-1107-4BB8-9D10-4F7ECAC923B2@gmail.com> (raw)
In-Reply-To: <CAHAc2jdUVXz+G6h82vMBVVsvr98a-eNsd7yfwz3Muva5UaajQg@mail.gmail.com>
References: <CAHAc2jdUVXz+G6h82vMBVVsvr98a-eNsd7yfwz3Muva5UaajQg@mail.gmail.com>
> On 1 Jun 2020, at 20:18, Shaheed Haque <shaheedhaque@gmail.com> wrote:
>
> Hi,
>
> I'm using Django's ORM to access Postgres12. My "MyModel" table has a JSONB column called 'snapshot'. In Python terms, each row's 'snapshot' looks like this:
>
> ======================
> snapshot = {
> 'pay_definition' : {
> '1234': {..., 'name': 'foo', ...},
> '99': {..., 'name': 'bar', ...},
> }
> ======================
>
> I'd like to find all unique values of 'name' in all rows of MyModel. I have this working using native JSON functions from the ORM like this:
>
> =====================
> class PayDef(Func):
> function='to_jsonb'
> template="%(function)s(row_to_json(jsonb_each(%(expressions)s->'pay_definition'))->'value'->'name')"
>
> MyModel.objects.annotate(paydef=PayDef(F('snapshot'))).order_by().distinct('paydef').values_list('paydef', flat=True)
> =====================
>
> So, skipping the ordering/distinct/ORM parts, the core looks like this:
>
> to_jsonb(row_to_json(jsonb_each('snapshot'->'pay_definition'))->'value'->'name’)
I do something like this to get a set of sub-paths in a JSONB field (no idea how to write that in Django):
select snapshot->’pay_definition’->k.value->’name’
from MyModel
join lateral jsonb_object_keys(snapshot->’pay_definition’) k(value) on true
I don’t know how that compares performance-wise to using jsonb_each, but perhaps worth a try. Obviously, the way it’s written above it doesn’t return distinct values of ’name’ yet, but that’s fairly easy to remedy.
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
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: haramrae@gmail.com, shaheedhaque@gmail.com
Subject: Re: Using JSONB with nested key-value structures, and skipping/wildcarding the key
In-Reply-To: <960EE185-1107-4BB8-9D10-4F7ECAC923B2@gmail.com>
References: <CAHAc2jdUVXz+G6h82vMBVVsvr98a-eNsd7yfwz3Muva5UaajQg@mail.gmail.com> <960EE185-1107-4BB8-9D10-4F7ECAC923B2@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