Export respondent information


Conjointly provides several ways to export respondent information and analysis.

Export respondent information via JSON

Here is how to use R to get respondent information via a JSON. There are two modes:

  • The simplified mode gives you respondents’ information and answers to survey questions only for responses currently used in the report, excluding any monadic blocks.
  • The full mode gives you all respondents’ information and answers to survey questions, including those from monadic blocks.
CONJOINTLY_TOKEN <- "YOUR_TOKEN" # Get a token from https://run.conjoint.ly/utilities/tokens

getRespondentsJSON <- function(experiment_id, mode = c('simplified', 'full'), colnameFormat = c("one_row", "split_rows"), TOKEN = CONJOINTLY_TOKEN) {

  library(httr)
  library(jsonlite)
  mode <- match.arg(mode)
  colnameFormat <- match.arg(colnameFormat)

  headers <- add_headers(
    `Authorization` = paste("Bearer", TOKEN),
    `Content-Type` = "application/json",
    `Accept` = "application/json",
    `X-Requested-With` = "XMLHttpRequest"
  )

  # Request export of data as JSON:
  exportRequest <- POST(
    paste0("https://api.conjoint.ly/api/report/", experiment_id, "/analysis/respondents/job"),
    headers,
    body = toJSON(list(
      "command" = "respondents",
      "mode" = mode,
      "colnameFormat" = colnameFormat
    ), auto_unbox = T)
  ) |> content("parsed", encoding = "UTF-8")

  # Make sure the export is completed:
  repeat {
    if (exportRequest$data$status %in% c('completed', 'done')) {
      break;
    }
    Sys.sleep(2)
    exportRequest <- GET(
      paste0("https://api.conjoint.ly/api/jobs/", exportRequest$data$id, "/get"),
      headers
    ) |> content("parsed", encoding = "UTF-8")
  }

  # Find the location of the file on AWS:
  intermediateFilename <- exportRequest$data$response$result$filename
  awsFileLocationRequest <- GET(
    paste0("https://api.conjoint.ly/api/report/", experiment_id, "/respondents?filename=", intermediateFilename),
    headers
  ) |> content("text", encoding = "UTF-8")

  # Download the file from AWS:
  dataRequest <- GET(awsFileLocationRequest) |> 
      content("text", encoding = "UTF-8") |>
      fromJSON()

  return(dataRequest)
}

JSONlist2DataTable <- function(JSONlist) {
  library(data.table)
  if (length(JSONlist$output)) {
    result <- do.call(cbind, JSONlist$output) |> data.table()
    colnames(result) <- unlist(JSONlist$columns)[as.numeric(colnames(result))]
    for (i in colnames(result)) {
      result[[i]][sapply(result[[i]], is.null)] <- NA
      result[[i]] <- unlist(result[[i]])
    }
  } else {
    result <- matrix(
      nrow = 0,
      ncol = length(result$columns),
      dimnames = list(NULL, unlist(result$columns))
    ) |> data.table()
  }
  result
}

result <- getRespondentsJSON(experiment_id) |> JSONlist2DataTable()

Export experiment data in Excel

Here is how to export all experiment data at once to an Excel file using R.

CONJOINTLY_TOKEN <- "YOUR_TOKEN" # Get a token from https://run.conjoint.ly/utilities/tokens

downloadExport <- function(
  experiment_id,
  overwrite = FALSE,
  filename = "data.xlsx",
  colnameFormat = "one_row", # Replace "one_row" with "split_rows" to get split rows format
  numberFormatPreference = TRUE,
  TOKEN = CONJOINTLY_TOKEN
) {

  library(httr)
  library(jsonlite)

  headers <- add_headers(
    `Authorization` = paste("Bearer", TOKEN),
    `Content-Type` = "application/json",
    `Accept` = "application/json",
    `X-Requested-With` = "XMLHttpRequest"
  )

  body <- list(
    type = "excel",
    colnameFormat = colnameFormat,
    numberFormatPreference = numberFormatPreference
  )

  cat('Requesting export of data...\n')
  exportRequest <- POST(
    paste0("https://api.conjoint.ly/api/experiments/", experiment_id, "/export/raw-data"),
    headers,
    body = toJSON(body, auto_unbox = TRUE)
  ) |> content("parsed", encoding = "UTF-8")

  repeat {
    if (exportRequest$data$status %in% c('completed', 'done')) {
      break
    }
    Sys.sleep(2)

    cat('Checking if file is ready...\n')
    exportRequest <- GET(
      paste0("https://api.conjoint.ly/api/jobs/", exportRequest$data$id, "/get"),
      headers
    ) |> content("parsed", encoding = "UTF-8")
  }

  cat('Finding the location of the file on AWS...\n')
  intermediateFilename <- exportRequest$data$response$uri
  awsFileLocationRequest <- GET(intermediateFilename, headers)

  cat('Downloading the file from AWS...\n')
  GET(
    awsFileLocationRequest$url,
    write_disk(filename, overwrite = overwrite)
  )

  return(awsFileLocationRequest$url)
}

downloadExport(experiment_id, overwrite = TRUE)

You can also export a specific data element rather than all data at once.

Export experiment data in SPSS format

You can use R to export your experiment data in SPSS format. There are two modes:

  • The full mode includes monadics and unused responses in the exported file.
  • The quick mode excludes monadics and unused responses in the exported file.
CONJOINTLY_TOKEN <- "YOUR_TOKEN" # Get a token from https://run.conjoint.ly/utilities/tokens

# Replace "full = TRUE" with "full = FALSE" to get quick mode
downloadExportSPSS <- function(experiment_id, full = TRUE, overwrite = FALSE, filename = "data.sav", TOKEN = CONJOINTLY_TOKEN)  {

  library(httr)
  library(jsonlite)

  headers <- add_headers(
    `Authorization` = paste("Bearer", TOKEN),
    `Content-Type` = "application/json",
    `Accept` = "application/json",
    `X-Requested-With` = "XMLHttpRequest"
  )

  if (full) {
    url <- paste0("https://api.conjoint.ly/api/experiments/", experiment_id, "/export/sav-full")
  } else {
    url <- paste0("https://api.conjoint.ly/api/experiments/", experiment_id, "/export/sav")
  }

  cat('Requesting export of data...\n')
  exportRequest <- POST(
    url,
    headers
  ) |> content("parsed", encoding = "UTF-8")

  # Make sure the export is completed:
  repeat {
    if (exportRequest$data$status %in% c('completed', 'done')) {
      break;
    }
    Sys.sleep(2)

    cat('Checking if file is ready...\n')
    exportRequest <- GET(
      paste0("https://api.conjoint.ly/api/jobs/", exportRequest$data$id, "/get"),
      headers
    ) |> content("parsed", encoding = "UTF-8")
  }

  cat('Finding the location of the file on AWS...\n')
  intermediateFilename <- exportRequest$data$response$uri
  awsFileLocationRequest <- GET(
    intermediateFilename,
    headers
  )

  cat('Downloading the file from AWS...\n')
  GET(
    awsFileLocationRequest$url,
    write_disk(filename, overwrite = overwrite)
  )

  return(invisible(awsFileLocationRequest$url))
}

downloadExportSPSS(exp_id, full = TRUE, overwrite = TRUE) # Replace "full = TRUE" with "full = FALSE" to get quick mode

Export experiment data in R format

You can export raw experiment data in R format with the following code.

CONJOINTLY_TOKEN <- "YOUR_TOKEN" # Get a token from https://run.conjoint.ly/utilities/tokens
exp_id <- 123456 #  Replace 123456 with the experiment_id you want to export your data from
downloadRawData <- function(experiment_id, filename = NULL, TOKEN = CONJOINTLY_TOKEN, env = rlang::caller_env()) {

  needLoading <- FALSE
  if (is.null(filename)) {
    filename <- tempfile(".Rdata")
    needLoading <- TRUE
  }
  
  library(httr)
  library(jsonlite)

  headers <- add_headers(
    `Authorization` = paste("Bearer", TOKEN),
    `Content-Type` = "application/json",
    `Accept` = "application/json",
    `X-Requested-With` = "XMLHttpRequest"
  )

  url <- paste0("https://api.conjoint.ly/api/experiments/", experiment_id, "/export/raw-rdata")

  cat('Requesting export of data...\n')
  exportRequest <- POST(
    url,
    headers
  ) |> content("parsed", encoding = "UTF-8")

  # Make sure the export is completed:
  repeat {
    if (exportRequest$data$status %in% c('completed', 'done')) {
      break;
    }
    Sys.sleep(2)

    cat('Checking if file is ready...\n')
    exportRequest <- GET(
      paste0("https://api.conjoint.ly/api/jobs/", exportRequest$data$id, "/get"),
      headers
    ) |> content("parsed", encoding = "UTF-8")
  }

  cat('Finding the location of the file on AWS...\n')

  intermediateFilename<- exportRequest$data$response$uri

  awsFileLocationRequest <- GET(
    intermediateFilename,
    headers
  )

  cat('Downloading the file from AWS...\n')

  GET(
    awsFileLocationRequest$url,
    write_disk(filename, overwrite = TRUE)
  )
  
  if(needLoading) {
    load(filename, envir = env)
  }

  return(invisible(awsFileLocationRequest$url))
}

downloadRawData(exp_id, filename = "datadump.Rdata") # save to Rdata file
downloadRawData(exp_id) # load to current environment

Caching behaviour

Of the exports on this page, only Raw Data (Excel) is cached. The rest are always generated fresh on each request:

ExportAPI commandCached?
Respondent InformationrespondentsNo, always generated fresh
Raw Data (Excel)export-raw-dataYes, cached
SPSS Quick Exportexport-savNo, always generated fresh
SPSS Full Exportexport-sav-fullNo, always generated fresh
RData Exportexport-raw-rdataNo, always generated fresh

For the cached export, results are stored after the first generation, and subsequent calls with the same parameters return the previously generated file until the cache is invalidated. To obtain fresh data reflecting new respondents or updated experiment settings, reset the report before re-requesting the export.

For a reusable R example, see Reset and regenerate a report.