Abram
Abram

Reputation: 59

Qualtrics API - Certain columns change from string to number

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 based 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 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.

This is what the data looks like when I use this program

This is what I want it to look like

Here is my code with my api credentials changed:

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)

#print (requests)

# 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 :)

Upvotes: 2

Views: 913

Answers (2)

Abram
Abram

Reputation: 59

Finally figured it out. Yes, adding the useLabels param was needed, but using the code I supplied above wasn't enough. For some reason JSON wouldn't accept it, I believe it needed a boolean to typecast into JSON not just a string that looks like a boolean. So I made a dictionary and loaded the params and used JSON.dumps() on the dictionary and it worked like a charm. Hope this helps anyone else wanting their data with the labels.

useLabels = True
dictionaryPayload = {'format': fileFormat, 'surveyId': surveyId, 'useLabels': useLabels}
downloadRequestPayload = json.dumps(dictionaryPayload)

Upvotes: 2

T. Gibbons
T. Gibbons

Reputation: 5004

Set the useLabels parameter to true: https://api.qualtrics.com/docs/create-response-export

Upvotes: 0

Related Questions