Reputation: 10626
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?
Upvotes: 0
Views: 69
Reputation: 514
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']]
Upvotes: 0
Reputation:
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']
Upvotes: 1