Hitting a KeyError when attempting to extract an array of survey data | XM Community
Solved

Hitting a KeyError when attempting to extract an array of survey data

  • 5 April 2018
  • 1 reply
  • 81 views

Badge
I've managed to cobble together a script from the Qualtrics samples that gets a list of all of the surveys associated with my apiToken and then exports all active surveys into a folder. Unfortunately, I've found that if I just create an array of all of my surveyIds where isActive == True, I get a KeyError on the following line (Python):
progressId = downloadRequestResponse.json()["result"]["id"]

If I run a check on ownerId, I don't get the KeyError. However, not all of my surveys are extracted. It's super weird and I'm a bit at a loss. Anyone have any ideas of how I might fix this?

~~~~
import os
import requests
import json
import zipfile
try: import simplejson as json
except ImportError: import json

Setting user Parameters


apiToken = 'APITOKEN'
dataCenter = 'DATACENTER'

Constructing the URL


baseUrl = "https://{0}.qualtrics.com/API/v3/surveys".format(dataCenter)
headers = {
"x-api-token": apiToken,
}

Getting the json data


json_data = requests.get(baseUrl,headers=headers).json()

Set the empty array


surveyIds = []
i = 0

Create the array; prints are for debugging only all active survey names and IDs


for each in json_data['result']['elements']:

If I don't include the OwnerID check, I get the KeyError, but if I do include the OwnerID check, I don't get all of my surveys


if each['isActive'] == True and each['ownerId'] == 'OWNERID':
surveyIds.append(each['id'])
print(each['name'])
print(each['id'])
print
i = i + 1

For debugging


print "There are ", i, " active surveys"
print(surveyIds)

Construct the survey extracts


for surveyId in surveyIds:
fileFormat = "csv"

# This the data center our qualtrics site uses.
dataCenter = 'DATACENTER'

# Setting static parameters
requestCheckProgress = 0
progressStatus = "in progress"
baseUrl = "https://{0}.qualtrics.com/API/v3/responseexports/".format(dataCenter)
headers = {
"content-type": "application/json",
"x-api-token": apiToken,
}

# Step 1: Creating Data Export
downloadRequestUrl = baseUrl
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '"}'
downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload, headers=headers)

This is where the error is thrown if I don't include the OwnerID check


progressId = downloadRequestResponse.json()["result"]["id"]
print downloadRequestResponse.text

# Step 2: Checking on Data Export Progress and waiting until export is ready
while requestCheckProgress < 100 and progressStatus is not "complete":
requestCheckUrl = baseUrl + progressId
requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"]
print "Download is " + str(requestCheckProgress) + " complete"

# Step 3: Downloading file
requestDownloadUrl = baseUrl + progressId + '/file'
requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True)

# Step 4: Unzipping file
with open("RequestFile.zip", "wb") as f:
for chunk in requestDownload.iter_content(chunk_size=1024):
f.write(chunk)
zipfile.ZipFile("RequestFile.zip").extractall("SurveyExports")
icon

Best answer by Emily 23 May 2018, 00:59

View original

1 reply

Userlevel 6
Badge +7
Hi @muriah! We're sorry that you were experiencing this issue. If you are still encountering this problem, we recommend reaching out to our support team and they can help you get this sorted!

Leave a Reply