Support returning null for missing leaves in a path
axsuul opened this issue · 6 comments
I'm not sure if this is supported already since I see that process_function_or_literal
accepts a default value. But basically, if we have JSON that looks like this
{
"data": [
{
"name" : "john",
"gender" : "male"
},
{
"name" : "ben"
}
]
}
then the path data[*].gender
will return (as expected)
["male"]
However, there are situations where a nil
value is preferred to be returned instead if the leaf/key is missing. So perhaps something like
JsonPath.new('data[*].gender', default: nil).on(object)
will instead return
["male", nil]
This is already supported actually in the Java library: https://github.com/json-path/JsonPath
I don't mind submitting a pull request that supports this unless you know of a quick solution, thanks!
Interesting stuff! Let me look into it. I'll tell you what's needed.
Uh, it's a bit crappy, because default
is not propagated properly anywhere. It's going to be a refactor with quite a size. @axsuul
Alright, thanks for investigating. We can close this for now I guess and revisit in the future 👍
As you wish. :) I'm looking into ways of doing it nicely though. I'll say when I come up with one. :)