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 do you extract data from json using python

I have this json file:

{
  "entityId": "12345",
  "displayName": "hostabc",
  "toRelationships": {
    "isProcessOf": [
      {
        "id": "proce123D00BB86",
        "type": "PROCESS_GROUP_INSTANCE"
      },
      {
        "id": "proc678DD0DBA4",
        "type": "PROCESS_GROUP_INSTANCE"
      },
      {
        "id": "proc978DD0DBA4",
        "type": "PROCESS_GROUP_INSTANCE"
      }
      
    ]
  }
}

I need to extract id fields under isProcessOf and build a data list.

I am new to python, how would I do this?

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

>Solution :

Looks like a list comprehension would do the job. You access dictionary values by its keys, so since you want to access 'id's of dictionaries in a list under the 'isProcessOf' key, which in turn is under the 'toRelationships' key, you can do it as:

out = [d['id'] for d in dct['toRelationships']['isProcessOf']]

Output:

['proce123D00BB86', 'proc678DD0DBA4', 'proc978DD0DBA4']
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