Skip to content

Commit fd8ceba

Browse files
committed
fix(jsonpath): assing value only if the item exists
1 parent 8e1ca7b commit fd8ceba

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

jsonpath_ng/jsonpath.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def make_datum(self, value):
5454
else:
5555
return DatumInContext(value, path=Root(), context=None)
5656

57+
5758
class DatumInContext(object):
5859
"""
5960
Represents a datum along a path from a context.
@@ -71,6 +72,7 @@ class DatumInContext(object):
7172
context within that passed in, so an object can be built from the inside
7273
out.
7374
"""
75+
7476
@classmethod
7577
def wrap(cls, data):
7678
if isinstance(data, cls):
@@ -436,7 +438,7 @@ def get_field_datum(self, datum, field):
436438
return AutoIdForDatum(datum)
437439
else:
438440
try:
439-
field_value = datum.value[field] # Do NOT use `val.get(field)` since that confuses None as a value and None due to `get`
441+
field_value = datum.value[field] # Do NOT use `val.get(field)` since that confuses None as a value and None due to `get`
440442
return DatumInContext(value=field_value, path=Fields(field), context=datum)
441443
except (TypeError, KeyError, AttributeError):
442444
return None
@@ -452,11 +454,11 @@ def reified_fields(self, datum):
452454
return ()
453455

454456
def find(self, datum):
455-
datum = DatumInContext.wrap(datum)
457+
datum = DatumInContext.wrap(datum)
456458

457-
return [field_datum
458-
for field_datum in [self.get_field_datum(datum, field) for field in self.reified_fields(datum)]
459-
if field_datum is not None]
459+
return [field_datum
460+
for field_datum in [self.get_field_datum(datum, field) for field in self.reified_fields(datum)]
461+
if field_datum is not None]
460462

461463
def update(self, data, val):
462464
for field in self.reified_fields(DatumInContext.wrap(data)):
@@ -465,7 +467,6 @@ def update(self, data, val):
465467
val(data[field], data, field)
466468
else:
467469
data[field] = val
468-
data[field] = val
469470
return data
470471

471472
def __str__(self):

0 commit comments

Comments
 (0)