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

Added hook for catching null value instead of empty list in path #31

Closed borysvorona closed 4 years ago

borysvorona commented 4 years ago

Fixes caused by errors during parsing non-perfect JSON files with different schema.

from jsonpath_ng.ext import parse

jsonpath_expr = parse("$.store.book[*].category")

json_file = """
{
    "store": {
        "book": null,
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}
"""
result = [
    match.value for match in jsonpath_expr.find(
        json.loads(json_file)
    )
]

For example, after execution code block above got a TypeError:

  File "/usr/local/lib/python3.7/site-packages/jsonpath_ng/jsonpath.py", line 255, in find
    for subdata in self.left.find(datum)
  File "/usr/local/lib/python3.7/site-packages/jsonpath_ng/jsonpath.py", line 255, in find
    for subdata in self.left.find(datum)
  File "/usr/local/lib/python3.7/site-packages/jsonpath_ng/jsonpath.py", line 257, in <listcomp>
    for submatch in self.right.find(subdata)]
  File "/usr/local/lib/python3.7/site-packages/jsonpath_ng/jsonpath.py", line 628, in find
    return [DatumInContext(datum.value[i], path=Index(i), context=datum) for i in xrange(0, len(datum.value))]
TypeError: object of type 'NoneType' has no len()
coveralls commented 4 years ago

Coverage Status

Coverage remained the same at ?% when pulling 3e36466972449bedf9ac572dfbb8115a3760d1d7 on borysvorona:master into 003a0b78f8a1857aa7027a837def54564a2b578a on h2non:master.

borysvorona commented 4 years ago

@h2non Could you take a look. It is a major issue. Also, I ask you to fix broken URLs in Travis CI build.