1

I'm trying to write some basic code to retrieve the list of workspaces and write the response to a file. I thought that this could be dumped to a JSON file?

Thanks for any help/suggestions.

I have taken the sample .py file and reworked it to look like this -

# Install the smartsheet sdk with the command: pip install smartsheet-python-sdk
import smartsheet
import logging
import os.path
import json

# TODO: Set your API access token here, or leave as None and set as environment variable "SMARTSHEET_ACCESS_TOKEN"
access_token = None

print("Starting ...")

# Initialize client
smart = smartsheet.Smartsheet(access_token)
# Make sure we don't miss any error
smart.errors_as_exceptions(True)

# Log all calls
logging.basicConfig(filename='rwsheet.log', level=logging.INFO)

response = smart.Workspaces.list_workspaces(include_all=True)
workspaces = response.data


with open('data.json', 'w') as outfile:
    json.dump(workspaces, outfile)


print("Done")

1 Answers1

0

I'm not sure what issue you are facing, but I think you would have an issue with the json.dump(workspaces, outfile) line as the results of that workspaces variable is a list that you would need to iterate through to get through the data. Using that variable will just print out the pointer with something like this: [<smartsheet.models.workspace.Workspace object at 0x10382a4e0>]
To work around this you would need to loop over the results of the variable and print them each out to a file. I found this post here about printing output to a file. The answer gives three approaches and I was able to get each of them to work with a loop iterating over the results.
One example:

import smartsheet

smar_client = smartsheet.Smartsheet(<ACCESS_TOKEN>)

response = smar_client.Workspaces.list_workspaces(include_all=True)
workspaces = response.data

with open('workspaces.json', 'w') as f:
  for workspace in workspaces:
    print(workspace, file=f)

Running this gave me a workspaces.json file in the same directory I ran my script from with the list of workspace objects.

daveskull81
  • 627
  • 4
  • 7