Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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'}
     ]
     }
 ]
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading