Felz Felz - 2 years ago 142
JSON Question

JSON output from Google Drive admin SDK activity.list()

I'm working on collecting users activity logs from Google Drive's Admin SDK. So far I did a python script and I'm getting some activity results in JSON format. But still, the format itself is not the one I'm looking for and here is an example:

{u'nextPageToken': u'A:6165487685465:-68949849879703739:37156465464:C65dE3qea', u'items': [{u'kind': u'admin#reports#activity', u'actor': {u'profileId': u'10651651515611643', u'email': u'mail@mail.com'}, u'events': [{u'type': u'login', u'name': u'login_success',...

As you can see, I'm getting some "u" characters in the beginning of every items and I don't know why.

Actually I want to get the output structured in this way:

"kind": "reports#activities",
"nextPageToken": string,
"items": [
"kind": "audit#activity",
"id": {
"time": datetime,
"uniqueQualifier": long,
"applicationName": string,
"customerId": string
"actor": {
"callerType": string,
"email": string,
"profileId": long,
"key": string
"ownerDomain": string,
"ipAddress": string,
"events": [
"type": string,
"name": string,
"parameters": [
"name": string,
"value": string,
"intValue": long,
"boolValue": boolean


Is it possible to structure it from the script itself ? (Maybe by using SimpleJson ?)

Here is my python code:

from __future__ import print_function
import httplib2
import os
import simplejson as json
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Reports API Python Quickstart'

def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
credential_path = os.path.join(credential_dir,

store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('admin', 'reports_v1', http=http)

results = service.activities().list(userKey='all', applicationName='drive',
activities = results.get('items', [])

if not activities:
print('No activities')
for activity in activities: print(results)

if __name__ == '__main__':

This is my first time using Google API and any help would be appreciated.

Thanks !

Answer Source

Don't let the unicode prefix scare you:

To get you json formatted, just use json.dumps(arg, indent=2) and the layout will be indented and spaced accordingly.


import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
                  indent=4, separators=(',', ': '))


    "4": 5,
    "6": 7

for more on python's JSON module here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download