Coder Social home page Coder Social logo

Comments (15)

mixja avatar mixja commented on August 16, 2024 20

One approach could be to store a reference to the original source data structure in a special variable (I was thinking $ might be a suitable identifier for this).

For example given the following input:

{
  "a": 5,
  "b": [
    {"c": 6},
    {"c": 7},
    {"c": 8}
  ]
}

It's currently impossible to generate something like this:

[
  {"a": 5, "b": 6},
  {"a": 5, "b": 7},
  {"a": 5, "b": 8}
]

With $ representing a reference to the source input, you could generate the above using the following expression:

b[].{a:$.a,b:c}

It might make sense to reset $ whenever you pipe an expression to another, given you are free to capture $ or a subtree of $ in the prior expression that is piped in.

I don't think this would be too difficult to implement - each reference to $ is another query that just needs to be evaluated from the source input.

from jmespath.js.

o0x2a avatar o0x2a commented on August 16, 2024 1

How about storing reference to parent object in each child node as we navigate from root to each child?
By storing the reference to the parent node we may not require path tracing.

from jmespath.js.

sauron918 avatar sauron918 commented on August 16, 2024 1

+1, possibility of access to parent object will be very useful in many cases

from jmespath.js.

simlu avatar simlu commented on August 16, 2024 1

If this is something you are looking to do, take a look at object-scan as an alternative to jmespath. The filter functionality is more limited, but it does provide a lot of search context

Disclaimer: I'm the author

from jmespath.js.

apatrida avatar apatrida commented on August 16, 2024

Adding a parent reference would require path tracing, which is fairly complicated to do especially through pipes and other transforms that reset the object to something fairly unrelated. I have been working on this for a port of the Java implementation and if I have successful experiments will push this back towards the spec. The issue is that such as small thing as this one function, completely makes implementation much more complicated.

In my version, I trace the query and have a full trace and the object at each step, so the trace acts as a way to get the path of any found node, but also since you have the trace, you can go upwards in a trace to find a parent. (assuming .. deep pathing traces each level it went through) ... The objects themselves typically have no upwards pointers to parents, so the engine has to know and it can via tracing.

from jmespath.js.

apatrida avatar apatrida commented on August 16, 2024

That is basically what tracing would do, but also crosses over pipes, collapsing arrays, and other steps.

Outside of tracing, yes parent node references could be kept from each point where you stop projection (i.e. pipe) so you can reach back up to that point. Basically a map from child->parent for each step of the tree.

from jmespath.js.

MarkSwanson avatar MarkSwanson commented on August 16, 2024

Fyi I'm unable to use JMESPath for a product I'm building - my requirements can only be met if I have '$' functionality.

from jmespath.js.

Allam76 avatar Allam76 commented on August 16, 2024

Using Symbol couls also be an option. Don't know how much extra complication that might lead to.

Path tracing might not be needed if we can put the parent object in the symbol. Something like this:

var parentSymbol = Symbol("parent");
obj[parentSymbol] = parent;

I've used this successfully in plain javascript.

from jmespath.js.

Thaina avatar Thaina commented on August 16, 2024

I want to propose @.. for parent access. Is it collide with any existing syntax?

from jmespath.js.

barbu110 avatar barbu110 commented on August 16, 2024

Hello! Anything on this, please?

from jmespath.js.

Thaina avatar Thaina commented on August 16, 2024

@barbu110 It seem jmespath just set in stone. No new feature will be added and become standardized as json. Currently now I lose hope in jmespath to add new feature and am working on new parser of my own. But I still don't have good syntax for parent accessor, really harder than I thought

from jmespath.js.

Allam76 avatar Allam76 commented on August 16, 2024

I use a method where preprocess and set a parent Symbol and then use a proxy as data input. Whenever the parent property comes up, the proxy then delegates to the symbol. This approch requires no changes to the library.

from jmespath.js.

sscots avatar sscots commented on August 16, 2024

@Allam76 Can you post an example of how you handled that?

from jmespath.js.

darrenmothersele avatar darrenmothersele commented on August 16, 2024

I added support for $ to access the root object in my fork of jmespath.js.

The example given above, with b[].{a:$.a,b:c} works, as you can see here:

https://codesandbox.io/s/upbeat-elgamal-xxz7s?fontsize=14&hidenavigation=1&theme=dark

I added $ as a new token (TOK_ROOT) to the lexer and parser, as you can see in this commit: daz-is@0d28f24

from jmespath.js.

Allam76 avatar Allam76 commented on August 16, 2024

Sure:
jsonata parent proposal both my solution and native implementation

from jmespath.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.