jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

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

Open jotes opened this issue 5 years ago • 3 comments

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!

jotes avatar Feb 06 '20 13:02 jotes

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

cburgmer avatar Feb 07 '20 21:02 cburgmer

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.

jotes avatar Feb 09 '20 22:02 jotes

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

cburgmer avatar Mar 06 '20 18:03 cburgmer