jmespath.py icon indicating copy to clipboard operation
jmespath.py copied to clipboard

Differentiate between missing values and null values

Open ntextreme3 opened this issue 4 years ago • 4 comments

I'm trying to do a search like:

from jmespath import search
data = {
    "grid": [
        {"col1": "a", "col2": None},
        {"col1": "b", "col2": None},
        {"col1": "c", "col2": "x"},
        {"col1": "d", "col2": "y"},
    ]
}
col1_data = search("grid[*].col1", data)  # gets ["a", "b", "c", "d"]
col2_data = search("grid[*].col2", data)  # gets ["x", "y"], expecting [None, None, "x", "y"]

If the col2 element did not exist, I think the current behavior makes sense. However, I think that search by default shouldn't also do filtering.

For reference: https://github.com/jmespath/jmespath.py/blob/599b0f7ea650a60f7512233301e70ff8151de0f0/jmespath/visitor.py#L134-L138 https://github.com/jmespath/jmespath.py/blob/599b0f7ea650a60f7512233301e70ff8151de0f0/jmespath/visitor.py#L279-L280

Thoughts ?

ntextreme3 avatar Jul 09 '20 22:07 ntextreme3

Just came across this myself. Would a PR be welcome?

zalmane avatar Dec 26 '22 21:12 zalmane

@zalmane @ntextreme3 you might be interested to learn that JMESPath Community supports this scenario - albeit currently in an indirect way.

springcomp avatar Mar 18 '23 07:03 springcomp

@zalmane @ntextreme3 you might be interested to learn that JMESPath Community supports this scenario - albeit currently in an indirect way.

That's completely different that what the OP asked...

The questions was how search can support None values as well, currently they are discarded.

cdogaru-hrp-ctr avatar Apr 05 '23 17:04 cdogaru-hrp-ctr

That's completely different that what the OP asked... The questions was how search can support None values as well, currently they are discarded.

Hum 🤔 then maybe I’m missing something…

search( map(&col2, grid), data ) -> [ null, null, "x", "y" ]

If you need more downstream processing, you can definitely distinguish between missing values vs null values by converting them temporarily for processing:

map(&col2 && col2 || 'value-was-null', grid)-> [ "value-was-null", "value-was-null", "x", "y" ]

Sure, this is kludgey, but that is definitely something that’s possible today.

springcomp avatar Apr 05 '23 19:04 springcomp