Sub topic 12
Use JSON output in scripts
This chapter assumes you have a default target org set, can run sf data query, and have a terminal that can run Bash. The examples also use jq, a separate command-line JSON processor. Install it from your package manager and check it with jq --version.
Why JSON output
By default, sf prints tables and colored text for people to read. That layout can change between CLI versions, so a script that parses it can break. Most sf commands accept the --json flag, which prints one JSON document in a stable shape. Your script reads fields from that document instead of scraping text.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --jsonThe shape of the output
Every --json response has the same top-level keys.
| Key | Meaning |
|---|---|
| `status` | `0` on success, a non-zero number on failure |
| `result` | The command's data. Its contents depend on the command |
| `warnings` | A list of warning messages |
For sf data query, result holds records, totalSize, and done. A failed command has no result. It reports name and message fields that describe the error, and the process exits with a non-zero code.
Extract values with jq
Pipe the output to jq and select the field you need. The -r flag prints raw strings without quotes.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --json \
| jq -r '.result.records[].Name'Other common filters:
# Number of records returned
sf data query --query "SELECT Id FROM Account" --json | jq '.result.totalSize'
# Username of the default org
sf org display --json | jq -r '.result.username'A script that handles errors
A reliable script checks whether the command succeeded before it reads result. In the script below, set -euo pipefail stops the script on the first failure. The || branch captures the error message from the JSON and sends it to standard error.
#!/usr/bin/env bash
set -euo pipefail
ORG="${1:-myorg}"
QUERY="SELECT Id, Name FROM Account LIMIT 5"
if ! output=$(sf data query --query "$QUERY" --target-org "$ORG" --json); then
echo "Query failed: $(echo "$output" | jq -r '.message')" >&2
exit 1
fi
count=$(echo "$output" | jq '.result.totalSize')
echo "Found $count accounts in $ORG:"
echo "$output" | jq -r '.result.records[] | "\(.Id) \(.Name)"'The script passes --target-org explicitly. In automation, an explicit org is safer than relying on whichever default is set on the machine.
Common errors
- Parsing human output. Piping a command without
--jsonintojqfails with a parse error. Add the flag. - Mixing messages into JSON. With
--json, the CLI sends machine-readable output to standard output. If you add your ownecholines to the same stream,jqcan no longer parse it. Send your own messages to standard error with>&2. - Ignoring failures. A failed command still prints JSON, so
jqmay run without error on an error document. Check the exit code or.statusfirst. - Assuming
resultis a list. Its shape depends on the command. Run the command once and read the JSON before you write the filter. - Missing
jq.command not foundmeansjqis not installed. It is not ansfproblem.
Exercise
A script runs sf data query --query "SELECT Id FROM Contact" --json | jq '.result.records' and prints null when the org login has expired. Why does it print null, and how should the script detect the problem?
Show answer
When the command fails, the JSON has no result key, so jq returns null for .result.records. The error details are in the name and message fields. The script should capture the output, check the exit code (or .status), and print .message to standard error before it exits, as in the error-handling script above.
Exercise
Write a command that prints only the names of the first 10 accounts from the default org, one per line.
Show answer
sf data query --query "SELECT Name FROM Account LIMIT 10" --json | jq -r '.result.records[].Name'