Using JMESPath Queries in the Globus CLI
The Globus CLI supports using JMESPath queries to filter and transform the output of commands. JMESPath is a query language for JSON with its own specification and syntax.
To learn more about JMESPath, consider reading the JMESPath Tutorial.
The --jmespath
Option
All Globus CLI commands support a --jmespath
option which takes a JMESPath
query as an argument.
The command’s JSON output will be processed with the query before being
emitted.
For example, it can be used to extract the task_id
from a globus transfer
task submission:
$ globus transfer $source $dest --batch $data --jmespath 'task_id'
"37fda17c-d07a-4004-8818-3067862ba8e3"
We also support --jq
, for "json query", as an alias for --jmespath
!
Combining --jmespath
with --format=UNIX
JMESPath output is still JSON data. That means that strings are quoted, arrays are wrapped in brackets, and commas are used as separators.
The CLI supports an alternative output format, designed to be combined with
--jmespath
to produce output in a format more easily consumed by standard
UNIX tools.
Using --format UNIX
will emit data with quotes stripped, using tabs and
newlines as separators.
For example, taking the example above and adding --format UNIX
:
$ globus transfer $source $dest --batch $data --jmespath 'task_id'
37fda17c-d07a-4004-8818-3067862ba8e3
In turn, this lets us use the output in a pipeline, subshell, or other context:
$ task_id="$(globus transfer $source $dest --batch $data --jmespath 'task_id')"
$ globus task wait $task_id --timeout 1800
$ globus task wait "$(globus transfer $source $dest --batch $data --jmespath 'task_id')" --timeout 1800
The --format
option is also available as -F
, supports "slamming", and is
case insensitive. So you can abbreviate --format=UNIX
as -F UNIX
or
-Funix
.
Exploring output with --format JSON
In order to use --jmespath
effectively, you need to know what the
JSON data for a given command is.
A great way to explore is to start with --format JSON
to see the raw JSON and
then to process that result with JMESPath queries.
For example, one might start with
$ globus group list --format JSON
...
to see the output, and then use JMESPath on the result. Perhaps to write a script:
#!/bin/bash
echo 'List My Groups:'
globus group list \
--jmespath '[].[name, id]' \
-Funix | \
awk -F'\t' '{print $1, "("$2")"}'