I am trying to output the metadata of some files in my dropbox in json format, using the below.
for entry in dbx.files_list_folder(path = '/TG/').entries:
print(entry.name)
path = ("/TG/" + str(entry.name))
data_output = {dbx.files_get_metadata(path=path)}
if '.py' in path:
print(data_output)
with open(write_file, 'w') as outfile:
json.dump(data_output, outfile)
It's outputting TypeError: Object of type set is not JSON serializable however.
Via the use of {
and }
, you're putting the result of the files_get_metadata
call into a set
, which is not JSON serializable. There doesn't seem to be any need for a set here in the code you shared, so you can just drop the {
and }
. That would leave the actual FileMetadata
type, though that's not actually JSON serializable either. You'll need to convert that to something that makes sense for your use case. E.g., most simply, just call str
on it.
Additionally, as written, your code is a bit redundant. The files_list_folder
method already gives you the same information you would get from files_get_metadata
, so you can drop the files_get_metadata
calls. The files_list_folder
method returns a list of the same Metadata
objects that files_get_metadata
returns. (Removing the files_get_metadata
calls should make your code run faster too.)
Also, you can get the path directly from the entry without reconstructing it.
With all of those changes, it would look like:
for entry in dbx.files_list_folder(path = '/TG/').entries:
print(entry.name)
if '.py' in entry.path_lower:
print(entry)
with open(write_file, 'w') as outfile:
json.dump(str(entry), outfile) # or whatever conversion or formatting you want instead of just str