This python code works for data pull except that it limits the output to the columns instance, submitter, device id, created at, updateat, reviewstate. What tweak do I need to pull all columns in the submissions pane.
def fetch_odk_data(base_url, username, password, project_id, form_id):
# Set the endpoint for fetching the data
endpoint = f"{base_url}/v1/projects/{project_id}/forms/{form_id}/submissions"
# Create a session to handle authentication
session = requests.Session()
session.auth = (username, password)
all_submissions = []
try:
# Make a GET request to fetch the data
response = session.get(endpoint)
response.raise_for_status() # Raise an error for bad responses
# Parse the JSON data
data = response.json()
# Debugging: print the structure of the response
print("Response Data Structure:", json.dumps(data, indent=4))
# Ensure we are correctly accessing submissions
if 'submissions' in data:
all_submissions.extend(data['submissions'])
else:
print("No 'submissions' key found in the response.")
# Check if there's pagination and fetch all pages if necessary
while 'nextPageToken' in data:
next_page = f"{endpoint}?pageToken={data['nextPageToken']}"
response = session.get(next_page)
response.raise_for_status()
next_data = response.json()
# Debugging: print the structure of the next page response
print("Next Page Data Structure:", json.dumps(next_data, indent=4))
if 'submissions' in next_data:
all_submissions.extend(next_data['submissions'])
else:
print("No 'submissions' key found in the next page response.")
# Save the complete submission data to a file
with open('locator_form.txt', 'w') as file:
json.dump(all_submissions, file, indent=4) # Write the list of submissions in a formatted way
print("Data successfully written to locator_form.txt")
return all_submissions
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
# Example usage
if __name__ == "__main__":
base_url = ODK_URL # Replace with your ODK Central base URL
username = ODK_USERNAME # Replace with your username
password = ODK_PASSWORD # Replace with your password
project_id = ODK_PROJECT_ID # Replace with your project ID
form_id = ODK_FORM_ID # Replace with your form ID
data = fetch_odk_data(base_url, username, password, project_id, form_id)
if data is not None:
print(data)