How to export a summary of test results to a CSV using Bash

The following example provides some bash scripts for using the mabl Reporting API batch results endpoint to retrieve a set of test run results and transform them into a CSV format.


To execute the example scripts you need a command line capable of executing bash scripts with a couple common utilities installed

  • curl
  • sed
  • jq: available for a variety of platforms from here

Retrieving test results

Save the following bash script as This script transforms the JSON results of the mabl batch results endpoint to a CSV format. You can edit the fields list in the script if you do not want all fields or want to change their order.


# Check if the -h flag was provided to get usage help
if [[ "$1" == "-h" || "$1" == "-help" || "$1" == "--help" || "$1" == "help" ]]; then
    echo "Transform mabl batch results JSON (provided on STDIN) into a CSV format with optional header row (included if -p provided as argument)"
    echo "Example usage: cat results.json | $0 -p > results.csv"
    exit 0

# Check whether the -p flag was provided to print the header
if [ "$1" == "-p" ]; then

# Extract the test_results array using jq
results=$(jq -r '.test_results')

# Define the list of variable names to extract

# Print the header as a comma-separated list of field names
if [ "$print_header" == true ]; then
    echo "${fields[*]}" | tr ' ' ','

# Loop through each result in the array and output a flat CSV format, using base64 encoding+decoding to handle spaces and special characters in the result rows during loop
for row in $(echo "${results}" | jq -r '.[] | @base64'); do
    decoded_row=$(echo ${row} | base64 --decode)

    for field in "${fields[@]}"; do
        value=$(echo $decoded_row | jq -r ".$field // empty") # extract the field, using the empty string instead of "null" for fields with no value
        value=$(echo "$value" | sed 's/,/_/g')  # replace commas with underscores

    # Join field values with comma separators
    printf -v joined '%s,' "${fields_values[@]}"
    echo "${joined%,}"

After saving the script, you can make it executable from the bash command prompt with: chmod +x



This script is not very speedy with the use of jq; it will likely take around one or two minutes to run per 100 run results.

Call the script from a bash command line to process results from the mabl Reporting API and save them to a CSV. Replace <WORKSPACE_ID> with your workspace ID and replace <API_KEY> with a mabl Viewer API key.

curl "<WORKSPACE_ID>/testRuns?advanced_metrics=true" \
-u "key:<API_KEY>" \
| ./ -p \
> results.csv

Retrieving additional results

In order to dump a large quantity of results into a file, you have to call the endpoint repeatedly to get multiple pages of results. You can create another bash script as, that uses the first script and does this paging for you:


# Check number of arguments and provide help with usage example if incorrect
if [[ $# -lt 3 || $# -gt 4 ]]; then
  echo "Usage: $0 <workspace ID> <API key> <output filename> [query parameter string]"
  echo "Example: $0 'MY-WORKSPACE-ID-w' 'API-KEY' 'results.csv' 'test_id=MY-TEST-ID-j&earliest_run_start_time=1677587852400'"
  exit 1


# Add given query parameters to API endpoint URL
if [ "$4" ]; then

# Set initial cursor value to null

# Add header row to output file
echo "" | ./ -p >> $output_file

# Loop until there is no more data to retrieve
while : ; do
  echo "Getting batch of results with cursor = $cursor"
  # Make API call with current cursor value
  if [[ $cursor != "null" ]]; then
    response=$(curl -s "$endpoint&cursor=$cursor" -u "key:$api_key")
    response=$(curl -s "$endpoint" -u "key:$api_key")

  # Extract cursor value from API response
  cursor=$(echo $response | jq -r '.cursor')

  # Process data from API response
  echo "Processing batch of results with cursor = $cursor"
  echo "$response" | ./ >> $output_file
  echo "Finished processing batch of results with cursor = $cursor"

  [[ $cursor != "null" ]] || break

After saving the script, make it executable from a bash command prompt with:
chmod +x

Call the script from the command line to retrieve multiple pages of results from the mabl Reporting API and save them to a CSV file:


The script takes the following arguments:

  • <WORKSPACE_ID>: replace <WORKSPACE_ID> below with your workspace ID
  • <API_KEY>: have a workspace owner create or use an existing mabl Viewer API key and replace <API_KEY> with the key secret.
  • <OUT_FILE>: replace <OUT_FILE> with the name of the output file, such as results.csv
  • `<QUERY_PARAMETER_STRING>: use this optional argument to filter your results.

See the API documentation for the API batch run results endpoint to find available query parameters and create an appropriate query parameter string to replace <QUERY_PARAMETER_STRING>.

For example, to specify all runs within a time range, use to get the desired start and end timestamp in milliseconds and use them with in a query parameter string as follows: earliest_run_start_time=1677506400000&latest_run_start_time=1677592800000