public inbox for [email protected]
help / color / mirror / Atom feedFrom: Alex R <[email protected]>
To: pgsql-novice <[email protected]>
Subject: Re: JSONpath query that returns paths to the matches
Date: Mon, 20 Dec 2021 01:19:43 +0100
Message-ID: <CANK1NR2=WvSBRAL=HKsZUHyoK_uTZKajwoE-bC=Q2GNHzSkv0g@mail.gmail.com> (raw)
In-Reply-To: <CAKFQuwYVhCVwa0qfTfpvkziLnj_ogdpGruJhFqJ=Zig=n2+LuA@mail.gmail.com>
References: <CANK1NR1E1CyHcqF+xQvBYBY0+G4GGL_HKDDSad2am74zz_N3mg@mail.gmail.com>
<CAKFQuwYVhCVwa0qfTfpvkziLnj_ogdpGruJhFqJ=Zig=n2+LuA@mail.gmail.com>
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 :-)
view thread (3+ messages)
reply
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 using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected]
Subject: Re: JSONpath query that returns paths to the matches
In-Reply-To: <CANK1NR2=WvSBRAL=HKsZUHyoK_uTZKajwoE-bC=Q2GNHzSkv0g@mail.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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox