sileht / python-jsonpath-rw-ext

Extensions for JSONPath RW
Apache License 2.0
59 stars 19 forks source link

Filter issue with multiple data types #28

Open corvust opened 4 years ago

corvust commented 4 years ago

Hello,

Attempting to use a filter to check for an int value when some elements may have dict values results in a exception. I would execpt differing data types to either be skipped, or for the filter components to be evaluated in order (i.e. value=0 is only evaluated if name=key1, see below example).

Sample code:

import jsonpath_rw_ext

json_data = {
    "elements":[
        {"name": "key1", "value": 0},
        {"name": "key2", "value":
            {"a": "", "b": "", "c": ""}
        }
    ]
}

jp_search = jsonpath_rw_ext.parse("$.elements[?(name=key1 & value=0)].value")
data = [match.value for match in jp_search.find(json_data)]
print(data)

Results in this error:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    data = [match.value for match in jp_search.find(json_data)]
  File "venv/lib/python3.6/site-packages/jsonpath_rw/jsonpath.py", line 226, in find
    for subdata in self.left.find(datum)
  File "venv/lib/python3.6/site-packages/jsonpath_rw/jsonpath.py", line 226, in find
    for subdata in self.left.find(datum)
  File "venv/lib/python3.6/site-packages/jsonpath_rw/jsonpath.py", line 228, in <listcomp>
    for submatch in self.right.find(subdata)]
  File "venv/lib/python3.6/site-packages/jsonpath_rw_ext/_filter.py", line 49, in find
    for i in moves.range(0, len(datum.value))
  File "venv/lib/python3.6/site-packages/jsonpath_rw_ext/_filter.py", line 52, in <listcomp>
    self.expressions))))]
  File "venv/lib/python3.6/site-packages/jsonpath_rw_ext/_filter.py", line 51, in <lambda>
    len(list(filter(lambda x: x.find(datum.value[i]),
  File "venv/lib/python3.6/site-packages/jsonpath_rw_ext/_filter.py", line 83, in find
    value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'