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
12 changes: 9 additions & 3 deletions src/jsonata/jsonata.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,21 @@ def evaluate_filter(self, predicate: Optional[Any], input: Optional[Any], enviro
results = utils.Utils.create_sequence()
if isinstance(input, utils.Utils.JList) and input.tuple_stream:
results.tuple_stream = True
if not (isinstance(input, list)):
if input is None:
# undefined input yields undefined output; skip filtering entirely
input = utils.Utils.create_sequence()
elif not (isinstance(input, list)):
input = utils.Utils.create_sequence(input)
if predicate.type == "number":
index = int(predicate.value) # round it down - was Math.floor
if index < 0:
# count in from end of array
index = len(input) + index
item = input[index] if 0 <= index < len(input) else None
if item is not None:
if 0 <= index < len(input):
item = input[index]
# Preserve JSON null at this index (vs. out-of-bounds, which is undefined)
if item is None:
item = utils.Utils.NULL_VALUE
if isinstance(item, list):
results = item
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/null_safety_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ def test_null_safety(self):

res = jsonata.Jsonata("$spread(null)").evaluate(None)
assert res is None

def test_array_index_preserves_null(self):
# Indexing into an array element that is JSON null must yield null,
# not be filtered out as if it were undefined.
data = {"data": [[1, None, 3], [2, None, 4], [3, None, 5]]}
res = jsonata.Jsonata("[$map(data, function($row) { $row[1] })]").evaluate(data)
assert res == [None, None, None]
Loading