Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jsonpath_ng/jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def _update_base(self, data, val, create):
return data

def filter(self, fn, data):
if data is not None:
if data is not None and isinstance(data, dict):
for field in self.reified_fields(DatumInContext.wrap(data)):
if field in data:
if fn(data[field]):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_
assert data_copy2 == expected_value


filter_test_cases = (
# Docs examples
("foo[*].baz", {'foo': [{'baz': 1}, {'baz': 2}]}, lambda d: True, {'foo': [{}, {}]}),
("foo[*].baz", {'foo': [{'baz': 1}, {'baz': 2}]}, lambda d: d == 2, {'foo': [{'baz': 1}, {}]}),
# Wildcard issue fix
("*.baz", {"flag": False, "foo": {"bar": 1, "baz": 2}}, lambda d: True, {"flag": False, "foo": {"bar": 1}}),
)


@pytest.mark.parametrize(
"expression, data, filter_function, expected_value",
filter_test_cases,
)
@parsers
def test_filter(parse: Callable[[str], JSONPath], expression: str, data, filter_function: Callable, expected_value):
data_copy = copy.deepcopy(data)
parse(expression).filter(filter_function, data_copy)
assert data_copy == expected_value


find_test_cases = (
#
# * (star)
Expand Down