I am exposing JSONPaths for advanced queries in my application, and sometimes it would be really convenient to chain expressions as I'd do for example with jq
. Example:
{
"foo": [
{
"bar": "bar",
"from": 10,
"to": 20
}
]
}
$.foo[*] ? (@.bar == "bar") | @.to - @.from
Now I see that the |
operator as used above is not implemented, and in pure SQL I could store the result of the first part in a table / variable. However in application I pass the JSONPath to jsonb_path_query
and it would be convenient if I didn't have to chain the invocations manually but just let PostgreSQL handle it all in one pre-baked statement. Is there anything that would let me do that?
EDIT: I could do that using e.g.
SELECT jsonb_path_query((
SELECT jsonb_path_query('{ ... }', '$.foo[*] ? (@.bar == "bar")')
), '$.to - $.from');
but that would mean that I'd need to split the JSONPath query (user input) - exactly what I don't want to do in the app.