Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jftGK-0002Ee-JW for pgsql-general@arkaria.postgresql.org; Mon, 01 Jun 2020 22:50:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jftGJ-0003zc-82 for pgsql-general@arkaria.postgresql.org; Mon, 01 Jun 2020 22:50:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jftGI-0003zV-Ub for pgsql-general@lists.postgresql.org; Mon, 01 Jun 2020 22:50:31 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jftGC-0004OA-8E for pgsql-general@postgresql.org; Mon, 01 Jun 2020 22:50:30 +0000 Received: by mail-ej1-x631.google.com with SMTP id gl26so10783233ejb.11 for ; Mon, 01 Jun 2020 15:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=7TO2hIz8sh18NLaxrLhpro7Q23Sz8GUdZHKpoEU/IiQ=; b=QJTPv7I5emU8zEJW/Vi6oVzukZvLRo9g0cBjq0/hVn/y31Ro64kr2WSkI0rizVVKnW F8D8nVSxAbKYITITxmIpKuzAYyzpqyqcDPQD9DE5J9LLhUw/FeLshPa+FCO9QVzvW7sp FXsHkTdxfHReZNd7faLMov7RotF5H0SHTx6U7arT75iOZupI8GMzj/9VLQbDdbv1dKBG HmiFhY6eL+bkRw+zNnk+GUkC87e+fCf9s5EaVXTjD7DBU9y35J65vjT2R9P3KRb0dy+8 hFcsgeF7h0JlbCbs4CnHN4jO+i4Igq5d5Ux+BdGbhAvOPCTYfzkHkVlumvurxyX11EFn Uj5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=7TO2hIz8sh18NLaxrLhpro7Q23Sz8GUdZHKpoEU/IiQ=; b=OZpmaV3Zq2QlVOcxnE9HFwC0gHnhgf9KEvBQfKI01hYOd11JZYcwqANkpCm9nNtXgI UUrHkZ8dLjSjXUbWfTqipapg6fveTztQRPm+viKzLd13rpjWqyLqRsbxLCYjnLrnMVOw GPiIwMkYyl5sdAibPifnFc4MLghflYWNbnXya9kUR8qRTdmixln+WEFgS/dVVRpXUbJo CUnqyTD93QaFjDcLNIpPUToWvHJuMHBfQN3iv4jnQaXiUQktXuepO5ShstaaIVhBpw53 GksioIneu7tMdrgbYVvVmhu+wNXhLAV9+fTjLMbl+THxcWPxrKK4F7S9EiJDQAcjObqx MAEA== X-Gm-Message-State: AOAM531nIJAlQPNMJBd8GjKZcAxpbCfWk95F0e521TTzpjXcOzPYiU1T tBds6W+RYVgAlbvmKGpx6aE= X-Google-Smtp-Source: ABdhPJys5in08UGyNlSE9yV1iqbfH8pgvFCMohjGB/5ZUHQ8mwhSB+tUAWJntk9vIknIvopkd+CFIw== X-Received: by 2002:a17:906:1ed2:: with SMTP id m18mr2990074ejj.529.1591051823192; Mon, 01 Jun 2020 15:50:23 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id n5sm591235edq.13.2020.06.01.15.50.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Jun 2020 15:50:21 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Using JSONB with nested key-value structures, and skipping/wildcarding the key From: Alban Hertroys In-Reply-To: Date: Tue, 2 Jun 2020 00:50:20 +0200 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <960EE185-1107-4BB8-9D10-4F7ECAC923B2@gmail.com> References: To: Shaheed Haque X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 1 Jun 2020, at 20:18, Shaheed Haque wrote: >=20 > Hi, >=20 > 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: >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > snapshot =3D { > 'pay_definition' : { > '1234': {..., 'name': 'foo', ...}, > '99': {..., 'name': 'bar', ...}, > } > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > 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: >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > class PayDef(Func): > function=3D'to_jsonb' > = template=3D"%(function)s(row_to_json(jsonb_each(%(expressions)s->'pay_defi= nition'))->'value'->'name')" >=20 > = MyModel.objects.annotate(paydef=3DPayDef(F('snapshot'))).order_by().distin= ct('paydef').values_list('paydef', flat=3DTrue) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > So, skipping the ordering/distinct/ORM parts, the core looks like = this: >=20 > = to_jsonb(row_to_json(jsonb_each('snapshot'->'pay_definition'))->'value'->'= name=E2=80=99) 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->=E2=80=99pay_definition=E2=80=99->k.value->=E2=80=99name=E2= =80=99 from MyModel join lateral jsonb_object_keys(snapshot->=E2=80=99pay_definition=E2=80=99= ) k(value) on true I don=E2=80=99t know how that compares performance-wise to using = jsonb_each, but perhaps worth a try. Obviously, the way it=E2=80=99s = written above it doesn=E2=80=99t return distinct values of =E2=80=99name=E2= =80=99 yet, but that=E2=80=99s 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.