0

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!

asked Feb 23, 2018 at 16:31

3 Answers 3

1

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')
answered Feb 23, 2018 at 16:35
Sign up to request clarification or add additional context in comments.

Comments

0

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))
answered Feb 23, 2018 at 16:41

Comments

0

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)
answered Feb 23, 2018 at 16:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.