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

Differentiate between missing values and null values #204

Open
ntextreme3 opened this issue Jul 9, 2020 · 4 comments
Open

Differentiate between missing values and null values #204

ntextreme3 opened this issue Jul 9, 2020 · 4 comments

Comments

@ntextreme3
Copy link

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:

def visit_field(self, node, value):
try:
return value.get(node['value'])
except AttributeError:
return None

if current is not None:
collected.append(current)

Thoughts ?

@zalmane
Copy link

zalmane commented Dec 26, 2022

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

@springcomp
Copy link

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

@cdogaru-hrp-ctr
Copy link

@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.

@springcomp
Copy link

springcomp commented Apr 5, 2023

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.

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

4 participants