Home > OS >  How can I access dictionaries values in python if I have a nested dictionary
How can I access dictionaries values in python if I have a nested dictionary

Time:02-25

I have this structure for a dictionary and I need to iterate over it to find if under the key 'items' and right after under the key field there is the value 'status' and the value 'In Progress' under the key 'toString'.

Also, I need to get the date it happened. This can be found one level higher looking for the value under the key 'created'.

I'd tried so many ways and nothing worked. I appreciate your help.

{'startAt': 0,
 'maxResults': 18,
 'total': 18,
 'histories': [{'id': '126221979',
   'author': {'self': '<some_URL>',
    'name': '<ID in the system>',
    'key': '<ID in the system>',
    'emailAddress': 'e-mail address',
    'avatarUrls': {'48x48': '<some_URL>',
     '24x24': '<some_URL>',
     '16x16': '<some_URL>',
     '32x32': '<some_URL>'},
    'displayName': 'Display Name',
    'active': True,
    'timeZone': 'America/Sao_Paulo'},
   'created': '2022-02-11T19:22:40.000 0000',
   'items': [{'field': 'Link',
     'fieldtype': 'jira',
     'from': None,
     'fromString': None,
     'to': '<Isue ID>',
     'toString': 'This issue child-of <Some ID>'}]},
  {'id': '126221981',
   'author': {'self': '<some_URL>',
    'name': '<ID in the system>',
    'key': '<ID in the system>',
    'emailAddress': 'e-mail address',
    'avatarUrls': {'48x48': '<some_URL>',
     '24x24': '<some_URL>',
     '16x16': '<some_URL>',
     '32x32': '<some_URL>'},
    'displayName': 'Display Name',
    'active': True,
    'timeZone': 'America/Sao_Paulo'},
   'created': '2022-02-11T19:23:56.000 0000',
   'items': [{'field': 'status',
     'fieldtype': 'jira',
     'from': 1,
     'fromString': 'Open',
     'to': 3,
     'toString': 'In Progress'}
     ]
     },
      {'id': '126221981',
   'author': {'self': '<some_URL>',
    'name': '<ID in the system>',
    'key': '<ID in the system>',
    'emailAddress': 'e-mail address',
    'avatarUrls': {'48x48': '<some_URL>',
     '24x24': '<some_URL>',
     '16x16': '<some_URL>',
     '32x32': '<some_URL>'},
    'displayName': 'Display Name',
    'active': True,
    'timeZone': 'America/Sao_Paulo'},
   'created': '2022-02-11T19:23:56.000 0000',
   'items': [{'field': 'status',
     'fieldtype': 'jira',
     'from': 1,
     'fromString': 'Open',
     'to': 3,
     'toString': 'In Progress'}
     ]
     }
 ]
}

CodePudding user response:

Use pd_json_normalize:

>>> pd.json_normalize(d['histories'], 'items', 'created')
    field fieldtype  from fromString         to                       toString                       created
0    Link      jira   NaN       None  <Isue ID>  This issue child-of <Some ID>  2022-02-11T19:22:40.000 0000
1  status      jira   1.0       Open          3                    In Progress  2022-02-11T19:23:56.000 0000
2  status      jira   1.0       Open          3                    In Progress  2022-02-11T19:23:56.000 0000
  • Related