1

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

Aaron
  • 65
  • 5
  • You can try the [pprint module](https://docs.python.org/3.8/library/pprint.html). It will still print the square brackets, but otherwise gives your wanted output. – Wups Nov 02 '20 at 14:27
  • Printing the data in tabular format might be better to read: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – Wups Nov 02 '20 at 14:49
  • 1
    I was not aware of either, thank you - something to explore – Aaron Nov 02 '20 at 15:20

1 Answers1

0

You can use this example to format and print the output:

for i in user_response['list']:
    print('Name: {}'.format(i['name']))
    print('Description: {}'.format(i['description']))
    print('Users:')
    print(',\n'.join(str(u) for u in i['users']))
    print()

Prints:

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'}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91