Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jpmckinney opened this issue May 4, 2024 · 0 comments
Closed

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

jpmckinney opened this issue May 4, 2024 · 0 comments

Comments

@jpmckinney
Copy link
Contributor

jpmckinney commented May 4, 2024

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

@jpmckinney jpmckinney changed the title If json_dict is not a dict, return a useful error If json_dict is not a dict, return a useful error/warning May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant