0

I don't know much about python and found an example that would serve my purpose with some modifications, currently trying to parse some data from a GET request response and I keep getting this error:

"activity['parameters']['initValue']))
TypeError: list indices must be integers or slices, not strTypeError: list indices must be integers or slices, not str"

JSON response looks like this:

{'kind': 'admin#reports#usageReports', 'etag': '"xxxxxxxxx/xxxxxxxxxxxx"', 'usageReports': [{'kind': 'admin#reports#usageReport', 'date': '2019-09-01', 'etag': '"xxxxxxxxx/xxxxxxxx"', 'entity': {'type': 'CUSTOMER', 'customerId': 'xxxxxxxx'}, 'parameters': [{'name': 'gmail:num_30day_active_users', 'intValue': '1234'}]}]}

Python Code:

    result = service.customerUsageReports().get(date='2019-09-01', parameters='gmail:num_30day_active_users').execute()
    
    results = result.get('usageReports', [])
    if not results:
         print('No data found.')
    else:
        print('Usage:')
        for activity in results:
            print(u'{0}: {1}'.format(activity['parameters'],
                activity['parameters']['intValue']))

What needs to be changed to make it work? Thank you!

0

1 Answer 1

1

from the data you provide, the value in parameters is a list of dict

'parameters': [{'name': 'gmail:num_30day_active_users',
    'intValue': '1234'}]}  

so you need to iterate all the items in list

result = service.customerUsageReports().get(date='2019-09-01', parameters='gmail:num_30day_active_users').execute()

results = result.get('usageReports', [])
if not results:
     print('No data found.')
else:
    print('Usage:')
    for activity in results:
       for line in activity['parameters']:
          print(u'{0}: {1}'.format(activity['parameters'],
            line['initValue']))
Sign up to request clarification or add additional context in comments.

1 Comment

@pyth0nnoOb same thing, you need to find the real key

Your Answer

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.