-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSONPath queries filtering on semantic annotations #387
Comments
Having spent a little longer looking at this I've realised that...
Should be...
(A silly syntax error).
I still haven't figured out an answer to this question. I've seen mention of an "in" operator from some sources which may help, but it doesn't seem to be part of the JSONPath specification and therefore is perhaps not widely supported? |
Ugh, it seems like the solution does indeed vary between different implementations. In "Goessner JSONPath" which evaluates JavaScript expressions, you would use:
But in the Jayway Java implementation you would use:
As far as I can tell there doesn't appear to be a standardised approach which works everywhere. This is quite a disappointing limitation given that searching for Things of a given |
@relu91 I just noticed that you implemented JSONPath search in Zion, using a library you wrote which converts JSONPath to SQL. Nice! That approach isn't going to work for me because I'm currently using a nosql database, but I'm curious whether you came across the searching through an array issue? If so, which approach did you take? I'm expecting at some point someone is going to tell me I need to implement the SPARQL-based search API in order to do this, which I am very much trying to avoid! (Though that may be the only way to properly resolve external ontologies used in semantic annotations). |
One issue here is that JSON Path implementations are not consistent, because there is no standard (yet). What we should do is give this feedback to the people working on the IETF spec, that it should work for JSON-LD with prefixes, etc. Proposed resolution: give feedback to IETF standards developers on this. What exactly should we say? |
I think there are two separate topics: 1. Standardised filter operators The first is to ensure there is a standardised mechanism to filter by the presence of a value in an array. There currently seem to be two different approaches taken by implementations for these kinds of filters:
I think my personal preference would be for the former, with an 2. JSON-LD support The second is about support for JSON-LD "compact IRIs". You can already filter by strings containing colons like
Supporting JSON-LD syntax like this is probably a much bigger topic, and possibly even a separate extension specification. |
Since doing this correctly will have to wait until the JSON Path IETF spec work is done, I have marked this issue as "Defer to Discovery 2.0" for now. Note: "2.0" is just a placeholder name, we have yet to decide on whether we will aim for 1.1 (compatible extensions) or 2.0 (breaking changes). In the meantime we should provide the above feedback to the IETF work and discuss this issue with the JSON-LD group. |
@benfrancis regarding your comments:
|
Hi all sorry for being unresponsive these days, but I am in the middle of my vacation time (I will be completely off next week).
Yes, we took this path (pun intended) to achieve better performance and to be closer to the current IETF JSONPath standard. The library closely follows draft 5 of the standard with a full compliant Parser but supporting only 90% coverage of the use cases. This is because a 1-1 translation between JSONPath and SQL/JSONPath (which to my surprise is an ISO standard) is not possible. Sadly, there are corner cases that we couldn't translate. If you are interested in the details we can discuss more in one of our calls when I'm back.
So yes, as you pointed out, the IETF JSONPath does not support any "include" operator in the current draft; at least from my understanding. I left a comment on one of the relevant issues about supporting nested filtering. Regarding how we tackled the issue, well there are some implementations that accept As for the other points in the discussion:
+1. For the next release, I would keep an eye on the support of the query language in DBs. We have to keep in mind that 90% of TDDs implementations out there will leverage some sort of DB to filter TDs. On JSONPath we already know that there is not a single DB implementation that supports it (at least we couldn't find any). On the other hand, there are plenty of solutions that use SQL/JSONPath or custom JSON filtering.
JSON-LD support will be nice to have moreover if we don't require a full-fledged SPARQL endpoint for it. Perhaps this will happen after ietf-wg-jsonpath/draft-ietf-jsonpath-base#203 discussion is settled. As @mmccool is pointing out, we can "incubate" an extension point like |
Thanks for explaining all of that, @relu91.
The conversation there is not looking promising, it sounds like filter operators like "in" or "includes" are unlikely to make it into the IETF specification, but they will also forbid arbitrary JavaScript expressions. So there may be no way to support the common use case of filtering by a value in an array. That limitation, combined with the issue of expanding JSON-LD prefixes, seems to suggest that neither of the use cases in my original question will be possible with the JSONPath search API (at least with the first version of the JSONPath IETF specification). Not being able to search the Directory for Things of a given I do have a crude implementation of the JSONPath search API using the jsonpath npm module which first fetches the full list of Things from the database and then filters the list afterwards (using the non-standard JavaScript expression approach), but apart from being non-standard that's obviously also very inefficient. I'd really like to avoid implementing a full SPARQL endpoint, because it will require significant architectural changes, full RDF processing capabilities, and possibly even a change of underlying database. Currently WebThings Gateway uses SQLite and an early prototype of WebThings Cloud uses MongoDB, but SPARQL would presumably work better with a graph database like Apache Marmotta or Amazon Neptune. For the time being I've had to implement a custom filtering API for Things which I can map directly onto MongoDB (or SQL) queries, e.g.
That's a shame as it won't be interoperable. |
Another option we have is to take the IETF JSONPath standard, then specify (and incubate) specific extensions, then include those in our standard. However, I hate to do that since it really needs to be in the IETF standard, and these do seem like common use cases, even in the non-IoT world! Maybe we need to look at XPath again. The only blocker there as I understood it was a lack of implementations (well, and a non-JSON-like-syntax, but oh well). |
BTW there is another issue: geolocation. There is probably another set of query extensions needed for geolocation, e.g. limiting queries to a certain area. There is a variant of the strategy in the previous comment we could use for both this and for Maybe we can also handle prefix expansion this way... e.g. include a filter that expands prefixes to URLs then contracts known URLs to new prefixes that we can then base queries on. Then we could include these additional queries in our API spec without touching JSONPath. |
@mmccool wrote:
I do think that defining a custom search API covering some common WoT use cases could be a reasonable approach if the JSONPath API is too limiting and the SPARQL API is very complex to implement and use. It could provide a more lightweight approach to carrying out basic semantic queries. I assume that would have to be targeted at the next version of the specification. |
Yes, this would be for the next version of the Discovery spec. I expect we will want to talk about this in the Pre-TPAC meetings next week (we need to roll up topics in our Discovery call on Monday, too...). As you said, having use-case-specific, chainable filters would also have the benefit of making "simple" queries simpler, while still allowing full query languages for complex cases. It would also make things like geolocation orthogonal to other kinds of queries (e.g. semantic) and query languages (SPARQL/JSONPath/XPath) if we also had geolocation-based filters. |
We just merged a PR that updated the JSONPath reference to the official RFC9535, see #542 |
I'm experimenting with using JSONPath queries to query a directory of Things.
For a simple query like finding a Thing by its title, this appears to work:
$[?(@.title=='foo')]
Now say I have a semantic annotation using the schema.org ontology, so I want to use a prefix in the property name. I would assume it would be something like:
$[?(@.'schema:floorLevel'=='G')]
At least with the online JSONPath evaluators I've tried, this is not accepted as a valid query. How can I query on a property with a colon in the name?
Going a step further, what if I want to look for Things whose
@type
array includes the valueiot:TemperatureSensor
?The text was updated successfully, but these errors were encountered: