Proper way of handling JSON Parsing TypeError when element does not exist

爱⌒轻易说出口 提交于 2021-01-29 04:18:15

问题


The code get's me what I want in the end. (which is to create a list of dictionary of the fields I want from a very large json dataset, so that I can create a dataframe for additional data processing)

However I have to construct a very large try/expect block to get this done. I am was wondering if there is a clearer/clever way of doing this.

The problem I'm having is that the details['element'] sometimes don't exist or have a value, which throws a NoneType exception if it does not exist on the child element['Value'] cannot be grabbed because it does not exist.

So I have a very large try/except block to set the variable to '' if that happens.

I tried to send the details['element'] to a function that would output a return value to the variable...but it looks like I can't do that, because Python checks if the element is a NoneType before passing it through the function, and this happens before sending it to the function.

Any thoughts?

rawJson = json.loads(data.decode('utf-8'))
issues = rawJson['issues']
print('Parsing data...')
for ticket in issues:
    details = ticket['fields']

    try:
        key = ticket['key']
    except TypeError:
        key = ''
    try:
        issueType = details['issuetype']['name']
    except TypeError:
        issueType = ''
    try:
        description = details['description']
    except TypeError:
        description = ''
    try:
        status = details['status']['name']
    except TypeError:
        status = ''
    try:
        creator = details['creator']['displayName']
    except TypeError:
        creator =''
    try:
        assignee = details['assignee']['displayName']
    except TypeError:
        assignee =''
    try:
        lob = details['customfield_10060']['value']
    except TypeError:
        lob ='' 

 .... There is a long list of this

回答1:


You can use get method which allows to provide a default value to simplify this code:

d = {'a': 1, 'c': 2}
value = d.get('a', 0) // value = 1 here because d['a'] exists
value = d.get('b', 0) // value = 0 here because d['b'] does not exist

So you can write:

for ticket in issues:
    details = ticket['fields']

    key = ticket.get('key', '')
    description = details.get('description', '')
    issueType = details['issuetype'].get('name') if 'issuetype' in details else ''

    ...


来源:https://stackoverflow.com/questions/42876705/proper-way-of-handling-json-parsing-typeerror-when-element-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!