New Tool Radar

Local AI

How to Pull Just the Answer Text Out of Ollama API Replies with jq

Pipe Ollama API replies through jq to pull out just the answer text, so a script can use the reply without the surrounding JSON.

Schematic diagram with three steps: ask the generate endpoint with streaming off, filter the reply with jq -r .response, and filter chat replies with jq -r .message.content.

An Ollama API reply is JSON: the answer sits inside a response or message field, surrounded by braces, quotes, timing numbers, and a done flag. That is fine for a program, and unreadable in a script that just wants the words. The jq command-line tool reads JSON and prints one field, so one pipe turns the whole reply into plain text you can save, print, or feed to the next command.

Pull the answer from a generate reply

Ask the local generate endpoint with streaming switched off, so the reply comes back as one JSON object instead of one object per line, and pipe it through jq. This is the example from a published Ollama shell guide, with its own prompt:

curl -s http://localhost:11434/api/generate -d '{

"model": "llama3",

"prompt": "Write a Bash one-liner to count total lines in all .sh files recursively.",

"stream": false

}' | jq -r '.response'

The -r flag prints raw text, without the JSON quotes around it. Without "stream": false, the API returns streaming JSON responses by default, one object per line, and jq -r '.response' prints each fragment as it arrives instead of one finished answer. The same "stream": false plus | jq shape appears in the published GitHub Actions workflow that calls /api/generate in CI.

Pull the answer from a chat reply

A chat reply wraps the answer one level deeper, inside message.content. The Ollama API introduction says to read the answer from message.content, and the local chat endpoint is http://localhost:11434/api/chat with a downloaded model and no authorization header. The filter is:

curl -s http://localhost:11434/api/chat -d '{"model": "llama3.2", "messages": [{"role": "user", "content": "Why is the sky blue?"}], "stream": false}' | jq -r '.message.content'

Use the generate filter for /api/generate and the chat filter for /api/chat; the wrong filter prints null, which is jq telling you the field is not there.

Try it: three checks from JSON to plain text

Check What you run What you should see
Raw reply The generate request without any pipe One JSON object with a response field, braces, and quotes
Filtered reply The same request piped to jq -r '.response' Only the answer text, one Bash one-liner, with no braces or quotes
Chat reply The chat request piped to jq -r '.message.content' Only the chat answer text about the sky

Expected result. We checked the Ollama shell guide, which gives the curl request and the jq -r .response pipe, the CI workflow, which gives the stream false request piped to jq, and the Ollama API introduction, which gives the local chat endpoint and the message.content field. We did not run curl or jq against an Ollama server on this machine.

Limits

jq has to be installed before the pipe works; without it the shell reports the command as not found. The -r flag matters: without it, jq '.response' prints the answer as a quoted JSON string, which is still JSON, not plain text. This guide covers reading answers only; writing JSON request bodies with jq and handling streaming output token by token are separate tasks. We did not test any of this ourselves.

Sources

See all