I'm running some simple GET requests against some software to extract and summarise data. I have been able to extract values from my rest API output and created a basic for loop to summarise the output. However I need to print it better so it is clearer for the end user. Hopefully the below description and examples will be able to show you what I'm trying to achieve.
user_response = user.json()
print(json.dumps(user_response, indent=2))
Rest output
user_response = {
"total": 14,
"offset": 0,
"limit": 25,
"list": [
{
"id": 1321,
"url": "/v1/domains/1001/usersets",
"name": "test1",
"description": "test rest 1",
"users": [
{
"uId": 1033,
"uName": "harry",
"gId": 1034,
"gName": "harry"
},
{
"uId": 1000,
"uName": "luke",
"gId": 1000,
"gName": "luke"
}
]
},
{
"id": 1257,
"url": "/v1/domains/1001/usersets",
"name": "test2",
"description": "test rest 2",
"users": [
{
"uName": "max"
},
{
"uName": "joe"
},
{
"uName": "polly"
},
{
"uName": "tara"
}
]
},
I was eventually able to extract the name, description and the user data but the output isn't the easiest to read for the average end user using the code below
user_response = user_response['list']
for x in user_response:
print("Name: ", x['name'],"\n", "Description: ", x['description'])
x = x['users']
print("Users: ", "\n", x, "\n")
Output in the terminal
Name: test1
Description: test rest 1
Users:
[{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'}, {'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}]
Name: test2
Description: test rest 1
Users:
[{'uName': 'max'}, {'uName': 'joe'}, {'uName': 'polly'}, {'uName': 'tara'}]
I was hoping to print a new line for every dictionary within the list for better reading however I cannot find anything on how to do that and my code edits have been inconclusive.
Name: test1
Description: test rest 1
Users:
{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'},
{'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}
Name: test2
Description: test rest 2
Users:
{'uName': 'max'},
{'uName': 'joe'},
{'uName': 'polly'},
{'uName': 'tara'}
Any ideas on how this can be achieved within my current for loop?
Also any tips on printing this data better would be much appreciated