Home > Software engineering >  How to select value of a key in nested dictionary? [closed]
How to select value of a key in nested dictionary? [closed]

Time:09-25

Below dictionary is stored into my variable. If 'error' is present, I want to print the error message "Error 1146: Table cust-comm-hist.EDW_CTC_HIST' doesn't exist"

{'endTime': '2021-09-22T16:14:59.525Z',
 'error': {'errors': [{'code': 'ERROR_RDBMS',
                       'kind': 'sql#operationError',
                       'message': 'Error 1146: Table '
                                  "'cust-comm-hist.EDW_CTC_HIST' doesn't "
                                  'exist'}],
           'kind': 'sql#operationErrors'},
 'insertTime': '2021-09-22T16:11:15.032Z',
 'kind': 'sql#operation',
 'name': '4ed21ee9-0ba5-4980-8873-0c117fec9792',
 'operationType': 'EXPORT',
 'startTime': '2021-09-22T16:11:16.596Z',
 'status': 'DONE',
 'targetId': 'cust-comm-hist-qa',
 'targetProject': 'llb-gcp-mktg-cust-comm-preprod}

CodePudding user response:

var['error']['errors'][0]['message']

CodePudding user response:

# if there is data
if dict['error']:
    # for each error
    for error in dict['error']['errors']:
        print(error)

or

if dict.get('error', None) is not None:
    # for each error
    for error in dict['error']['errors']:
        print(error)
  • Related