Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3672

Using postgresql JSONB functions with RFC-9535 compliant Json Path expressions [closed]

$
0
0

We're developing a spring boot 3 application with a postgresql (v15.8) database. This is what our database schema looks like:

CREATE TABLE test(    id             SERIAL PRIMARY KEY,    document       JSONB);

We would like our API to accept RFC-9535 compliant json path expressions and use them to fetch information from the database and on somewhat rare occasions, update it. The problem is that the current tools and functions that postgres provides, support json path expressions with a slightly different syntax than the ones defined in RFC-9535.

For example, a conventional json path expression would be:

$.document.phone[?(@.number==3616290989)]

This format of json path expressions is widely used and supported by libraries like jayway, for example. However, postgresql's functions like JSONB_PATH_QUERY don't work with this json path semantics, but rather has one of its own. So to run JSONB_PATH_QUERY successfully, the json path expression above must be "translated" to:

$.document.phone[*]?(@.number==3616290989)

And then the query:

select jsonb_path_query(document, '$.document.phone[*]?(@.number==3616290989)') from testwhere id = 1;

using the translated path expression, returns the desired result successfully. Note that the difference isn't that big, however in order to make postgresql's embedded JSONB functions work, we would need to write a custom translation from the first type of expression to the second one I've mentioned. With more complex expressions this will quickly become a nightmare to implement and maintain.

We explored some options that are the following:

  1. we considered passing postgresql compatible json path expressions, but it's not an option for us, as we'd really like to avoid binding our API to a specific database.

  2. we tried using a helper library (jayway more specifically) , but it's also not an option, as we would then be applying the json path expressions in memory, which, apart from the memory footprint, mostly defeats the purpose of using JSONB, because we won't be able to use the built-in postgresql mechanisms that help us traverse the document.

  3. implementing our own translation of RFC compliant json path expressions to postgresql compliant json path expressions (or basic JSON operators, like -> and @>), but we quickly realised that it would be hard to maintain and to make sure all possible cases would be covered.

We also looked for other options that could help us use RFC-9535 compliant json path expressions for embedded postgresql JSONB functions, but without any luck.

So, question is, is there any tool or approach that can help us achieve this? Any help, opinion or suggestion is highly appreciated!


Viewing all articles
Browse latest Browse all 3672

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>