Qualtrics API - Certain columns change from string to number | XM Community
Solved

Qualtrics API - Certain columns change from string to number

  • 25 May 2018
  • 5 replies
  • 195 views

I am downloading data from a survey that we did in Qualtrics. I found Python 3 sample code (https://api.qualtrics.com/docs/response-exports) and base my code around it.

After successful download I noticed that questions that had provided a list of option for the user to select from were downloaded as numbers (I suspect the index of the answer selected).

I've attached two images - I would like the data to look like the image with words, not like the image without the words.

I believe the answer is simple as putting in some parameters to download the data differently but I can't seem to find it in Qualtric's API documentation.

Here is my code with my api credentials changed. My notes are marked by // instead of # because Qualtrics renders those as headers.
import shutil
import os
import requests
import zipfile
import json
import io

//Setting user Parameters
apiToken = "myKey"
surveyId = "mySurveyID"
fileFormat = "csv"
dataCenter = "az1"

// 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)
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 the file
zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall("MyQualtricsDownload")

// Step 5: Move the file out of the folder and place it in the working directory --> change the paths to the appropiate paths for the server
shutil.move( "/Users/Abram/Documents/PCC/MyQualtricsDownload/Mindshare English v21.csv", "/Users/Abram/Documents/PCC/Mindshare English v21.csv")
os.rmdir("/Users/Abram/Documents/PCC/MyQualtricsDownload/")

print('Complete')
Thanks 🙂
icon

Best answer by LaurenK 9 July 2018, 23:06

View original

5 replies

Userlevel 7
Badge +27
Set the useLabels parameter to true: https://api.qualtrics.com/docs/create-response-export
am I right to add it to my downloadRequestPayload? like so:

useLabels = "true"

downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '","useLabels":"' + useLabels + '"}'

When I do it get the following error:

File "qualtrics.py", line 28, in <module>
progressId = downloadRequestResponse.json()["result"]["id"]
KeyError: 'result'
Userlevel 7
Badge +27
Try useLabels without the single quotes. It is a boolean, not a string.
When run as a string the requests module returns this error:

File "qualtrics.py", line 26, in <module>
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '","useLabels":"' + useLabels + '"}'
TypeError: must be str, not bool


I believe it is because python is putting together a JSON file to be sent to the qualtrics api using exclusively strings. When Qualtrics receives it as a JSON it then recognizes it as a bool datatype.
Userlevel 7
Badge +13
Hi @abrhim! It looks like the answer to your question can be found here! 🙂

Leave a Reply