0

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?

user1471980
  • 10,127
  • 48
  • 136
  • 235

2 Answers2

1

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']
0

I would use the json package in python and then read the data using list comprehension:

import json
file_dir = "<YOUR_FILE_DIR>"
with open(file_dir ) as f:
    data = json.load(f)
id_list = [process['id'] for process in data['toRelationships']['isProcessOf']]
Sascha Kirch
  • 466
  • 2
  • 3
  • 19