I am using an API to get JSON data from an external source and am not sure how to get particular elements from the JSON.
json_data = json.loads(resp.text) #resp.text is the response from the API call
print(json_data)
Prints the following (the raw output is single line/flat, so I have formatted to make it more readable):
[
{'version': {
'rowVersion': 2044},
'name': 'Administrator',
'permissions': [
{'path': 'Activity/Cancel All', 'name': 'Activity/Cancel All'},
{'path': 'Activity/View All', 'name': 'Activity/View All'},
{'path': 'Audit Log/Edit', 'name': 'Audit Log/Edit'},
{'path': 'Audit Log/View', 'name': 'Audit Log/View'}
],
'id': 1,
'permissionPaths': ['Activity/Cancel All', 'Activity/View All', 'Audit Log/Edit', 'Audit Log/View'],
'displayName': 'Administrator'
},
{'version': {
'rowVersion': 964033},
'name': 'ViewOnly',
'permissions': [
{'path': 'Activity/View All', 'name': 'Activity/View All'},
{'path': 'Audit Log/View', 'name': 'Audit Log/View'},
],
'id': 4,
'permissionPaths': ['Activity/View All', 'Audit Log/View']
'displayName': 'ViewOnly'
}
]
The following
for ROLES in json_data:
print(str(ROLES["name"]))
Prints:
Administrator
ViewOnly
So far, so good... However, I would like the output to be the role and permission name along the lines of...
Administrator, Activity/Cancel All
Administrator, Activity/View All
Administrator, Audit Log/Edit
Administrator, Audit Log/View
ViewOnly, Activity/View All
ViewOnly, Audit Log/View
Please let me know how I can achieve this in Python. Thanks!
3 Answers 3
This should help.
# -*- coding: utf-8 -*-
json_data = [
{'version': {
'rowVersion': 2044},
'name': 'Administrator',
'permissions': [
{'path': 'Activity/Cancel All', 'name': 'Activity/Cancel All'},
{'path': 'Activity/View All', 'name': 'Activity/View All'},
{'path': 'Audit Log/Edit', 'name': 'Audit Log/Edit'},
{'path': 'Audit Log/View', 'name': 'Audit Log/View'}
],
'id': 1,
'permissionPaths': ['Activity/Cancel All', 'Activity/View All', 'Audit Log/Edit', 'Audit Log/View'],
'displayName': 'Administrator'
},
{'version': {
'rowVersion': 964033},
'name': 'ViewOnly',
'permissions': [
{'path': 'Activity/View All', 'name': 'Activity/View All'},
{'path': 'Audit Log/View', 'name': 'Audit Log/View'},
],
'id': 4,
'permissionPaths': ['Activity/View All', 'Audit Log/View'],
'displayName': 'ViewOnly'
}
]
for ROLES in json_data:
for n in ROLES["permissions"]:
print(ROLES["name"], n["name"])
Output:
('Administrator', 'Activity/Cancel All')
('Administrator', 'Activity/View All')
('Administrator', 'Audit Log/Edit')
('Administrator', 'Audit Log/View')
('ViewOnly', 'Activity/View All')
('ViewOnly', 'Audit Log/View')
Comments
There are many solutions to this task. The next shows the use of list comprehension.
for role in json_data:
print("\n".join(["{role.name}, {permission.name}".format(role=role, permission=permission) for permission in role.permissions]))
You can also add a additional for-loop
for role in json_data:
for permission in role.permissions:
print("{role.name}, {permission.name}".format(role=role, permission=permission))
Comments
Here is my suggestion, that allows you to get what you want in a list through a one-liner style code. The for loop is only here to print each element of the list, if needed:
myList = ["{0}, {1}".format(ROLES["name"], perm["name"]) for ROLES in json_data for perm in ROLES['permissions']]
for i in myList:
print(i)