public inbox for [email protected]  
help / color / mirror / Atom feed
JSONpath query that returns paths to the matches
3+ messages / 2 participants
[nested] [flat]

* JSONpath query that returns paths to the matches
@ 2021-12-16 15:56 Alex R <[email protected]>
  2021-12-16 16:08 ` Re: JSONpath query that returns paths to the matches David G. Johnston <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Alex R @ 2021-12-16 15:56 UTC (permalink / raw)
  To: [email protected]

Hi,

I am trying to figure out whether Postgres 14 allows me to find the paths
to certain values in the JSON, rather than the values themselves. To
illustrate, here is a test query:

SELECT JSONB_PATH_QUERY($${
  "this": "that",
  "that": [{"x": "aaa"},{"y": "missed"}],
  "nested": {
        "deep": {
        "x": "bbb"
      }
  }
}$$, 'strict $.**."x"');

It returns 2 matches: "aaa" and "bbb". However, what I'd like to get
instead is 2 paths that point to the matches, i.e.,:
- $.that[0]
- $.nested.deep

Can this be accomplished using means that are available out of the box? If
not, what would be a sane way of implementing it?


Thank you for your help,
Alex


^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: JSONpath query that returns paths to the matches
  2021-12-16 15:56 JSONpath query that returns paths to the matches Alex R <[email protected]>
@ 2021-12-16 16:08 ` David G. Johnston <[email protected]>
  2021-12-20 00:19   ` Re: JSONpath query that returns paths to the matches Alex R <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2021-12-16 16:08 UTC (permalink / raw)
  To: Alex R <[email protected]>; +Cc: pgsql-novice <[email protected]>

On Thu, Dec 16, 2021 at 8:57 AM Alex R <[email protected]> wrote:

> It returns 2 matches: "aaa" and "bbb". However, what I'd like to get
> instead is 2 paths that point to the matches, i.e.,:
> - $.that[0]
> - $.nested.deep
>
> Can this be accomplished using means that are available out of the box? If
> not, what would be a sane way of implementing it?
>

1. Not that I can see documented.

2a See if some other language that has a PL extension for PostgreSQL can do
this more easily and write a function in the language.
2b Create a custom C language function based off of one of the existing
"path exists" functions that keeps track of its position in the graph and
the returns that instead of a boolean/whatever.
2c Create your own procedural code, which can be done in pl/pgsql, that
performs the graph traversal and captures the data like in 2b.

David J.


^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: JSONpath query that returns paths to the matches
  2021-12-16 15:56 JSONpath query that returns paths to the matches Alex R <[email protected]>
  2021-12-16 16:08 ` Re: JSONpath query that returns paths to the matches David G. Johnston <[email protected]>
@ 2021-12-20 00:19   ` Alex R <[email protected]>
  0 siblings, 0 replies; 3+ messages in thread

From: Alex R @ 2021-12-20 00:19 UTC (permalink / raw)
  To: pgsql-novice <[email protected]>

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 :-)


^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-12-20 00:19 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16 15:56 JSONpath query that returns paths to the matches Alex R <[email protected]>
2021-12-16 16:08 ` David G. Johnston <[email protected]>
2021-12-20 00:19   ` Alex R <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox