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

You can flexibly export experiment data in Excel format. Here is how to use R to get respondent information, conjoint analysis results, and other data via an Excel file.

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

formulateElements <- function() {
  elements <- list(
          type = "excel",
          colnameFormat = "one_row", # Replace "one_row" with "split_rows" to get split rows format
          numberFormatPreference = TRUE,
          elements = list(
                  list(
                          type = "special-entity", 
                          payload = list(
                                  what = "respondent-info", 
                                  mode = "simplified"   # Replace "simplified" with "full" to get full mode
                          )), 
                  list(type = "special-entity", payload = list(what = "conjoint-data")),  # This only works for conjoint experiments
                  list(type = "special-entity", payload = list(what = "data-dumps"))
  ))
  return(elements)
}

downloadExport <- function(experiment_id, overwrite = FALSE, filename = "data.xlsx", elements = formulateElements(), 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"
  )

  cat('Requesting export of data...\n')
  exportRequest <- POST(
    paste0("https://api.conjoint.ly/api/experiments/", experiment_id, "/export/element"),
    headers,
    body = toJSON(elements, 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)

    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)

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 <- 342589 #  Replace 342589 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