Get machine-readable JSON output

headless & scripting

// what it does

--output-format json wraps the run in a single JSON object with a response string and a stats block (per-model token counts, tool calls, files changed). Parse it with jq to extract just the answer or to track usage. The default format is text; json is the machine-friendly alternative.

// shell

$ gemini -p "list the exported functions" --output-format json | jq -r '.response'
$ gemini -p "explain schema" -o json | jq '.stats.models'

// gotcha

On failure the object carries an error field with type, message, and code instead of a normal response — check for it before reading .response or your script parses a null. Official docs list only text and json as output formats.

// resources