Cast Dropbox class to JSON
Question:
I am using the Python Dropbox package to pull team events:
dbx = dropbox.DropboxTeam(self.access_token)
result = dbx.team_log_get_events()
I need to pass the value of result
along as a JSON object. When I try using json.dumps(result)
, I get this error:
TypeError: Object of type GetTeamEventsResult is not JSON serializable
Is there a way to cast to json/dict with the Dropbox package?
My other potential solutions would be to write my own method to cast it (seems tedious) or just make API calls instead of using the package.
Answers:
Just heard back from a Dropbox Developer on the DB Forums:
The methods for calling the API in the Dropbox Python SDK don’t return JSON-serializable objects (or the original JSON from the server), but I’ll pass this along as a feature request. I can’t promise if or when that might be implemented though.
You can transform the information in the returned native object however you wish, but you’d need to write the code to do so though unfortunately, as you mentioned.
Will keep this post updated if more information comes out.
Depending on the attributes that the team_log_get_events()
returns, you can make a function to cast them to defaultdict()
For example from code I used to return dictionary of shared link meta data compatible with JSONResponse:
from collections import defaultdict
import dropbox
client = dropbox.Dropbox(DBX_TOKEN)
def shared_link_info(shared_link_url):
"""Information of the shared link provided."""
temp = defaultdict(dict)
result = client.sharing_get_shared_link_metadata(shared_link_url)
temp["id"] = result.id
temp["name"] = result.name
temp["content_type"] = type(result).__name__
temp["modified_date"] = result.client_modified.strftime("%Y-%m-%d")
temp["allow_download"] = result.link_permissions.allow_download
temp['path_lower'] = result.path_lower
if result.size:
temp["size"] = result.size
else:
result["size"] = None
if result.expires:
temp["link_expires"] = result.expires.strftime("%Y-%m-%d")
else:
result["link_expires"] = None
return temp
I am using the Python Dropbox package to pull team events:
dbx = dropbox.DropboxTeam(self.access_token)
result = dbx.team_log_get_events()
I need to pass the value of result
along as a JSON object. When I try using json.dumps(result)
, I get this error:
TypeError: Object of type GetTeamEventsResult is not JSON serializable
Is there a way to cast to json/dict with the Dropbox package?
My other potential solutions would be to write my own method to cast it (seems tedious) or just make API calls instead of using the package.
Just heard back from a Dropbox Developer on the DB Forums:
The methods for calling the API in the Dropbox Python SDK don’t return JSON-serializable objects (or the original JSON from the server), but I’ll pass this along as a feature request. I can’t promise if or when that might be implemented though.
You can transform the information in the returned native object however you wish, but you’d need to write the code to do so though unfortunately, as you mentioned.
Will keep this post updated if more information comes out.
Depending on the attributes that the team_log_get_events()
returns, you can make a function to cast them to defaultdict()
For example from code I used to return dictionary of shared link meta data compatible with JSONResponse:
from collections import defaultdict
import dropbox
client = dropbox.Dropbox(DBX_TOKEN)
def shared_link_info(shared_link_url):
"""Information of the shared link provided."""
temp = defaultdict(dict)
result = client.sharing_get_shared_link_metadata(shared_link_url)
temp["id"] = result.id
temp["name"] = result.name
temp["content_type"] = type(result).__name__
temp["modified_date"] = result.client_modified.strftime("%Y-%m-%d")
temp["allow_download"] = result.link_permissions.allow_download
temp['path_lower'] = result.path_lower
if result.size:
temp["size"] = result.size
else:
result["size"] = None
if result.expires:
temp["link_expires"] = result.expires.strftime("%Y-%m-%d")
else:
result["link_expires"] = None
return temp