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 1mz6PH-0000YB-K0 for pgsql-novice@arkaria.postgresql.org; Mon, 20 Dec 2021 00:19:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mz6PG-0005nQ-GQ for pgsql-novice@arkaria.postgresql.org; Mon, 20 Dec 2021 00:19:58 +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 1mz6PG-0005nB-7z for pgsql-novice@lists.postgresql.org; Mon, 20 Dec 2021 00:19:58 +0000 Received: from mail-io1-xd2d.google.com ([2607:f8b0:4864:20::d2d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mz6PE-000657-AE for pgsql-novice@lists.postgresql.org; Mon, 20 Dec 2021 00:19:57 +0000 Received: by mail-io1-xd2d.google.com with SMTP id p23so11120640iod.7 for ; Sun, 19 Dec 2021 16:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=MZAz7rQedb8uZ6I0lEHagRXp56RsTNrOBiHCYafd5lw=; b=qWTTTIsi+d2WpGGfHyWjJvVsJVcPMwkd7n/BQNg2iDnn+75Lgz+2LR1dipgbF3X/dR PZPyEp5FE4JuHk6RgQHC1MAUCFoDlufjFZSfFXxCQ4Xpz0zNzQ8o/COU8prudU3m71U9 5C4XfgBekwVp7x9nDcjm6KvveDrZ+WXB/1IHlbPktDJEuAZ47XUCkHolGghkWz4T3EmR 3RJ09gqVs2ogO9jpbB1RVIVlzMQqg0sr2N6wqiisNvmDzdsUCC1VrJyZh1NttFnyYG6z Uk0fe3NfupSBWRqxkpo/LByTo2aQL/UA4Wtbac1aTXRNxPwticd5RE4rTWGrUu5Zmbh3 LEjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=MZAz7rQedb8uZ6I0lEHagRXp56RsTNrOBiHCYafd5lw=; b=y/S8UJpPuwpaU4ub7HjtZkK2UmUzJ3mohUAJUkctKYEfgr/LJ0K4leoYyvLnCMIDJv zL3hL1UKXh0jvbfj+WZHDe9l8+bWLmT1I435nYVQsrdVVBdmBM8nTdPNhUUBS8hTyCu7 g+9DWXmDDyANSpSFjnlakFrzJ7sNtyHyCbCDqKHwvQWA4f7mnLfTyuKFIJfuICrUuuZ5 VQSkb89VH2v8BBgGS+AsZHGcYErA2qKB+BJg0kNnX4ail8hQyJp0bILmP0Rz/MpMtaOx 4WyygKQcOINTnIKcaH+l+psdboFMFpn0Wt06iBqo1i/AEnztkRtT7s4Q8RGuRMNyK7yZ 5Jhg== X-Gm-Message-State: AOAM531pglbpJRdJOVLVPHn1Vj66yg9WV2dxURoxMYcorDYMPIsHrUBp tk5uEojReeIYn98gk3LRiT5BNlUR3FM0hkMdr5+9pU/2Dtw= X-Google-Smtp-Source: ABdhPJw7JHmr8YqKmbWZxAxZJqeMP/CZzo0TeOk/zF/s1sUuk3xgmEIYbK21tT8SP3N1TKhvH+IEDJUD2HPeoz7Wsu4= X-Received: by 2002:a6b:3b43:: with SMTP id i64mr6955341ioa.182.1639959594317; Sun, 19 Dec 2021 16:19:54 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alex R Date: Mon, 20 Dec 2021 01:19:43 +0100 Message-ID: Subject: Re: JSONpath query that returns paths to the matches To: pgsql-novice Content-Type: multipart/alternative; boundary="00000000000010b9d905d388d90c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000010b9d905d388d90c Content-Type: text/plain; charset="UTF-8" Hi David, Thank you for the swift reply. After ruling out the option of doing that by leveraging JSONPath, I took a different approach and wrote a function in pl/pgsql that does not need these paths at all. I could deviate a bit, because I had some wiggle room in the context of the bigger problem I was working on. For others who might be confronted with a similar problem in the future, here is a minimal example that uses Python and a library called jsonpath_ng, which tells you what the match is and where it occurs: ``` # pip install jsonpath_ng import json from jsonpath_ng import jsonpath, parse raw = '''{ "this": "that", "that": [{"x": "aaa"},{"y": "missed"}], "nested": { "deep": { "x": "bbb" } } }''' data = json.loads(raw) query = parse('$..x') results = query.find(data) for entry in results: print(f'{entry.full_path} -> {entry.value}') # that.[0].x -> aaa # nested.deep.x -> bbb ``` Porting this to pl/python is left as an exercise for the reader :-) --00000000000010b9d905d388d90c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi David,

Thank you for the = swift reply. After ruling out the option of doing that by leveraging JSONPa= th, I took a different approach and wrote a function in=C2=A0pl/pgsql that does not need these = paths at all. I could deviate a bit, because I had some wiggle room in the = context of the bigger problem I was working on.


For others who might be co= nfronted with a similar problem in the future, here is a minimal example th= at uses Python and a library called=C2=A0jsonpath_ng, which tells yo= u what the match is and where it occurs:

```
# pip install jsonpath_ng

i= mport json
from jsonpath_ng import jsonpath, parse

raw =3D '&= #39;'{
=C2=A0 "this": "that",
=C2=A0 "th= at": [{"x": "aaa"},{"y": "missed&qu= ot;}],
=C2=A0 "nested": {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "= ;deep": {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "x": "bbb"= ;
=C2=A0 =C2=A0 =C2=A0 }
=C2=A0 }
}'''

data =3D= json.loads(raw)
query =3D parse('$..x')

results =3D quer= y.find(data)
for entry in results:
=C2=A0 =C2=A0 print(f'{entry.f= ull_path} -> {entry.value}')

# that.[0].x -> aaa
# nest= ed.deep.x -> bbb
```

Porting this to pl/python is left as an exercise for the reader :-)
--00000000000010b9d905d388d90c--