Skip to content

Commit 5dadd0b

Browse files
committed
fix non exists key cause exception
1 parent 705bc3e commit 5dadd0b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

jsonpath_rw/jsonpath.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def update(self, data, val):
525525

526526
def exclude(self, data):
527527
for field in self.reified_fields(DatumInContext.wrap(data)):
528-
if field in data:
528+
if data and field in data:
529529
del data[field]
530530
return data
531531

tests/test_jsonpath.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
22
import unittest
3+
import json
34

45
from jsonpath_rw import jsonpath # For setting the global auto_id_field flag
56

@@ -71,8 +72,6 @@ def test_DatumInContext_in_context(self):
7172
# assert AutoIdForDatum(DatumInContext(value=3, path=Fields('foo')),
7273
# id_field='id',
7374
# context=DatumInContext(value={'id': 'bizzle'}, path=This())).pseudopath == Fields('bizzle').child(Fields('foo'))
74-
75-
7675

7776
class TestJsonPath(unittest.TestCase):
7877
"""
@@ -526,6 +525,25 @@ def test_include_child(self):
526525
({'foo': {'bar': 1, 'baz': 2}}, 'non', {}),
527526
])
528527

528+
def test_exclude_not_exists(self):
529+
self.check_exclude_cases([
530+
(
531+
{
532+
'foo': [
533+
{'bar': 'bar'},
534+
{'baz': None}
535+
]
536+
},
537+
'foo.[*].baz.not_exist_key',
538+
{
539+
'foo': [
540+
{'bar': 'bar'},
541+
{'baz': None}
542+
]
543+
},
544+
),
545+
])
546+
529547
"""
530548
def test_include_where(self):
531549
self.check_include_cases([

0 commit comments

Comments
 (0)