Skip to content

Commit 7ec0556

Browse files
authored
Merge pull request kennknowles#33 from abloomston/nullObjs
Return empty result instead of crashing on index into None
2 parents 6b89738 + 02e0e5a commit 7ec0556

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

jsonpath_rw/jsonpath.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class Index(JSONPath):
477477
JSONPath that matches indices of the current datum, or none if not large enough.
478478
Concrete syntax is brackets.
479479
480-
WARNING: If the datum is not long enough, it will not crash but will not match anything.
480+
WARNING: If the datum is None or not long enough, it will not crash but will not match anything.
481481
NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
482482
"""
483483

@@ -487,7 +487,7 @@ def __init__(self, index):
487487
def find(self, datum):
488488
datum = DatumInContext.wrap(datum)
489489

490-
if len(datum.value) > self.index:
490+
if datum.value and len(datum.value) > self.index:
491491
return [DatumInContext(datum.value[self.index], path=self, context=datum)]
492492
else:
493493
return []

tests/test_jsonpath.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def test_index_value(self):
130130
self.check_cases([
131131
('[0]', [42], [42]),
132132
('[5]', [42], []),
133-
('[2]', [34, 65, 29, 59], [29])
133+
('[2]', [34, 65, 29, 59], [29]),
134+
('[0]', None, [])
134135
])
135136

136137
def test_slice_value(self):

0 commit comments

Comments
 (0)