Skip to content
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

Is it possible to find all nodes that have a certain key? #24

Open
jotes opened this issue Feb 6, 2020 · 3 comments
Open

Is it possible to find all nodes that have a certain key? #24

jotes opened this issue Feb 6, 2020 · 3 comments

Comments

@jotes
Copy link

jotes commented Feb 6, 2020

Hello,
I use jsonpath in one of my projects. It works perfectly for most of my use-cases.
Nonetheless, I need to do something like this:

$..[? @.example ]

So, basically I want to get (recursively) all nodes that have a property named example.
Is that query possible in jsonpath?
I've tried a couple of similar queries and they didn't work for me - no results found.

I don't know if that's a feature, a bug or something that requires hacking GVal expressions?
Could you help me/point in a good direction?
Thanks in advance!

@cburgmer
Copy link

cburgmer commented Feb 7, 2020

Does $..[?(@.example)] work for you? For the input {"example": [{"example": null}], "key": {"example": {"example": 42}}} this implementation returns [{"example": 42}].

@jotes
Copy link
Author

jotes commented Feb 9, 2020

Hey @cburgmer, thanks for your comment. Here's an example which should give you more context about my initial question:
https://play.golang.org/p/rB4wROddiNP
I would be glad if you could look at this. I don't know if the structure of the JSON objects in my projects is weird (?) Maybe there's an error in my way of using jsonpath.

@cburgmer
Copy link

cburgmer commented Mar 6, 2020

If I understand correctly you expect both examples to behave the same, but for a string value no match is found, while a match is found for a number.
This matches my own experiments, you can compare here: https://cburgmer.github.io/json-path-comparison/#filter_expression_with_value

This implementation only matches for true and numbers: https://cburgmer.github.io/json-path-comparison/results/filter_expression_with_value.html#Golang_github.com-PaesslerAG-jsonpath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants