PaesslerAG / jsonpath

BSD 3-Clause "New" or "Revised" License
172 stars 37 forks source link

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

Open jotes opened 4 years ago

jotes commented 4 years ago

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 commented 4 years ago

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

jotes commented 4 years ago

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 commented 4 years ago

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