h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
582 stars 85 forks source link

Updating a JSON document fails if one of many keys within an path has null value #47

Closed back2root closed 4 years ago

back2root commented 4 years ago

Steps to reproduce:

import json
from jsonpath_ng import parse

json_document = json.loads('{"elements":[{"sub": {"key":"1"}}, {"sub": null}, {"sub": {"key":"3"}}]}')
jsonpath_expr = parse("$.elements[*].sub.key")

print(jsonpath_expr.update(json_document, "--"))

Expected

# python test.py
{u'elements': [{u'sub': {u'key': '--'}}, {u'sub': None}, {u'sub': {u'key': '--'}}]}

Observed

# python test.py
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    print(jsonpath_expr.update(json_document, "--"))
  File "/opt/splunk/etc/apps/TA-json_redact/lib/jsonpath_ng/jsonpath.py", line 261, in update
    self.right.update(datum.value, val)
  File "/opt/splunk/etc/apps/TA-json_redact/lib/jsonpath_ng/jsonpath.py", line 526, in update
    if field in data:
TypeError: argument of type 'NoneType' is not iterable