awesome cli.

Use JSON output to connect CLI commands in a script

Read Asana task IDs with jq and pass them to another CLI command. A Bash example checks JSON shape, handles empty results and stops on errors.

Use jq to extract fields from a CLI’s JSON output before passing them to another command. Check the producer’s exit status and JSON shape before running the next step.

This example reads up to ten Asana tasks, then fetches each task by its GID. It uses the unofficial Asana CLI v0.1.2 and Bash. Awesome CLI’s maintainer also maintains the CLI. Follow the setup guide before running it.

Read task IDs and fetch each task

Save this as a Bash script. Replace the example project name with your project’s name:

set -euo pipefail
umask 077

asana task list --project "Launch Plan" --limit 10 --json > tasks.json

jq -r '
  if type != "array" then error("expected a task array")
  else
    .[] | .gid |
    if type == "string" and test("^[0-9]+$") then .
    else error("expected a numeric task GID")
    end
  end
' tasks.json > task-ids.txt

while IFS= read -r gid; do
  asana task view "$gid" --json
done < task-ids.txt

The first command reads tasks. The filter requires an array and numeric GID strings before the loop starts. An empty array produces no IDs, so the loop does nothing. Each iteration prints a separate JSON response.

The quoted "$gid" passes the ID as one argument. The filter excludes newlines and other characters that do not belong in an Asana GID. These commands read tasks; they do not update them.

umask 077 restricts access to newly created output files on Unix systems. The files contain project data, so keep them out of your repository.

Inspect the output before choosing a filter

To look at the saved JSON, run:

jq . tasks.json

The filter above uses the Asana CLI’s task-list result, an array with a gid field on each task. Another CLI might return an object with a nested array. Check its documented format before reusing the filter.

Asana CLI’s optional --include-meta flag wraps machine output in an object with data and meta. The example omits that flag. If you add it, adjust the filter to read the data field.

The jq manual explains -r, which prints strings without JSON quotes, and error, which makes this filter fail on an unexpected result.

Stop when an earlier command fails

The example uses Bash’s set -e so a failed list command or filter stops the script before the loop. If one task read fails in the loop, the script stops there. The Bash manual documents the contexts where this option applies.

pipefail also makes a pipeline fail when an earlier command fails. The example saves data before processing it, but that option matters if you later put the producer and jq in a pipeline.

If you replace the task reads with updates, check the target and preview each supported write with --dry-run. The preview guide explains what that flag checks.