Sub topic 6
Scripting with JSON output and jq
This chapter assumes you have the CLI installed, authenticated, and a working profile, and that you know basic shell pipes. Here you turn CLI output into data that other scripts can consume.
Why JSON output
By default the CLI prints tables meant for people. Column widths and headers can change between versions, so scripts that parse them break. Add --output json (or -o json) and the CLI prints structured JSON. Tools like jq can read it reliably.
Reading output with jq
jq is a command-line JSON processor. It reads JSON on standard input and applies a filter. These filters cover most automation work:
| Filter | Meaning |
|---|---|
| `.` | The whole input |
| `.[]` | Each element of an array |
| `.name` | The `name` field of an object |
| `.settings.name` | A nested field |
| `select(cond)` | Keep only items where the condition is true |
| `-r` flag | Print raw strings without quotes |
The following command lists each job as a tab-separated ID and name:
databricks jobs list --output json | jq -r '.[] | "\(.job_id)\t\(.settings.name)"'The -r flag removes the JSON quotes. The \( ... ) syntax inserts a value into a string.
Filtering
Use select to narrow results. This prints the IDs of jobs whose name starts with nightly_:
databricks jobs list --output json \
| jq -r '.[] | select(.settings.name | startswith("nightly_")) | .job_id'You can capture the result in a shell variable and loop over it:
for id in $(databricks jobs list -o json \
| jq -r '.[] | select(.settings.name | startswith("nightly_")) | .job_id'); do
echo "Found job $id"
doneData flow
Common errors
- Parsing the default table output. Always pass
--output jsonwhen a script reads the result. - Forgetting
-r. Without it,jqleaves quotes around strings, so"123"is not the same as123in a later command. - Assuming a list is always present. If the workspace has no jobs, some commands print nothing or
null. Use.[]?to avoid an error on missing arrays. - Mixing stdout and stderr. Progress and error messages go to standard error. Redirect with
2>/dev/nullonly when you are sure you do not need them. - Ignoring exit codes. In a pipeline, a failure in the first command can be hidden by
jqsucceeding. Start scripts withset -o pipefail.
A safer script pattern
This script stops on the first error and fails if a required value is empty:
#!/usr/bin/env bash
set -euo pipefail
job_id=$(databricks jobs list --output json \
| jq -r '.[] | select(.settings.name == "demo_job") | .job_id' \
| head -n 1)
if [ -z "$job_id" ]; then
echo "demo_job not found" >&2
exit 1
fi
echo "demo_job has ID $job_id"The check matters because an empty job_id would otherwise be passed to the next command and produce a confusing error.
Exercise
Write a command that prints only the names of jobs whose name contains the text "etl", one per line, without quotes. What goes wrong if you leave out -r?
Show answer
databricks jobs list --output json | jq -r '.[] | select(.settings.name | contains("etl")) | .settings.name'. Without -r, jq prints each name wrapped in double quotes, such as "daily_etl", which breaks later commands that expect the plain name.