h2non / jsonpath-ng

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

update_or_create fails when root object of original object is a list #108

Open rolling-robot opened 2 years ago

rolling-robot commented 2 years ago

Root object and first path element are inconsistent, yet update_or_create returns (and modifies original object!)

> pp = jsonpath_ng.ext.parse('$[0].section')
> pp.update_or_create({}, 1)
[{'section': 1}]

Now I'm asking jsonpath_ng to change the previous value to 2, but I get an error:

> pp.update_or_create([{'section': 1}], 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pete/.pyenv/versions/3.9.9/lib/python3.9/site-packages/jsonpath_ng/jsonpath.py", line 289, in update_or_create
    return _clean_list_keys(data)
  File "/home/pete/.pyenv/versions/3.9.9/lib/python3.9/site-packages/jsonpath_ng/jsonpath.py", line 763, in _clean_list_keys
    for key, value in dict_.items():
AttributeError: 'list' object has no attribute 'items'

Seems like a bug in handling first path element.