OpenDataServices / flatten-tool

Tools for generating CSV and other flat versions of the structured data
http://flatten-tool.readthedocs.io/en/latest/
MIT License
105 stars 15 forks source link

If `json_dict` is not a dict, return a useful error/warning #442

Closed jpmckinney closed 3 months ago

jpmckinney commented 6 months ago

JSONParser.parse() calls self.parse_json_dict(json_dict) which eventually tries for key, value in json_dict.items():, which can raise: AttributeError("'list' object has no attribute 'items'")

For example, if a user accidentally double-wraps OCDS releases in arrays:

{
  "releases": [[
    {"id": "1"}
  ]]
}

My preference would be something like:

from flattentool.exceptions import DataErrorWarning

# ...

class JSONParser(object):

# ...

    def parse(self):
        for num, json_dict in enumerate(self.root_json_list):
            if json_dict is None:
                # This is particularly useful for IATI XML, in order to not
                # fall over on empty activity, e.g. <iati-activity/>
                continue

            # NEW CODE
            if not isinstance(json_dict, dict):
                warn(_(f"The value at index {num} is not a JSON object"), DataErrorWarning)
                continue

Related: #398