schibsted / jslt

JSON query and transformation language
Apache License 2.0
638 stars 120 forks source link

Obtaining the complete path / pointer in current context #324

Open candicerimba opened 10 months ago

candicerimba commented 10 months ago

Is there a function we could use to access the complete path / pointer in a current context?

{
   "data": [
        {
            "field": "a",
            "value": "b"
         },
         {
            "field": "a",
            "value": "b" // access the complete path here, as something like /data/0/value
         },
}
larsga commented 10 months ago

There is no such function at the moment, no.

How are you proposing that it would work? What is the argument to the function?

What would you use it for?

candicerimba commented 10 months ago

I don't think we'd be able to implement it as a function? It feels like it'd require something deeper in the code, kind of how we access .field, we'd be tracking the parent .fields traversed up until that point. I haven't read JSLT code at all, so this might be wholly impossible to do.

We're currently using JSLT to detect certain fields / data, but the action we need to take doesn't happen with JSLT, so we'd ideally want to return a path / pointer to that entry for easy access.

larsga commented 10 months ago

Yes, it looks awkward to do as a function. One problem is that Jackson's JsonNode doesn't have any way to get the parent of the node, so taking a value and returning a JSLT expression to produce is not straightforward.

It probably is possible to extend the JSLT implementation to track the path taken down to the current value, by creating an ExecutionContext or something like that. It might not always yield correct results, however, and there would be a performance penalty to be paid by all JSLT users.