Skip to content

Commit e73507e

Browse files
authored
Merge pull request #36 from rayokota/fix-array-null
Preserve JSON null when indexing into an array
2 parents 172f018 + 518dc28 commit e73507e

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/jsonata/jsonata.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,21 @@ def evaluate_filter(self, predicate: Optional[Any], input: Optional[Any], enviro
512512
results = utils.Utils.create_sequence()
513513
if isinstance(input, utils.Utils.JList) and input.tuple_stream:
514514
results.tuple_stream = True
515-
if not (isinstance(input, list)):
515+
if input is None:
516+
# undefined input yields undefined output; skip filtering entirely
517+
input = utils.Utils.create_sequence()
518+
elif not (isinstance(input, list)):
516519
input = utils.Utils.create_sequence(input)
517520
if predicate.type == "number":
518521
index = int(predicate.value) # round it down - was Math.floor
519522
if index < 0:
520523
# count in from end of array
521524
index = len(input) + index
522-
item = input[index] if 0 <= index < len(input) else None
523-
if item is not None:
525+
if 0 <= index < len(input):
526+
item = input[index]
527+
# Preserve JSON null at this index (vs. out-of-bounds, which is undefined)
528+
if item is None:
529+
item = utils.Utils.NULL_VALUE
524530
if isinstance(item, list):
525531
results = item
526532
else:

tests/null_safety_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ def test_null_safety(self):
3131

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

0 commit comments

Comments
 (0)