How to Parse JSON in Bash with jq

← All guides

Bash has no built-in JSON parser — use jq, the standard command-line JSON processor. Pipe JSON into jq with a filter to extract or transform values.

Extract a field

Pipe JSON to jq and select a path. Use -r for raw (unquoted) string output.

bash
echo '{"name":"Ada","roles":["admin","dev"]}' | jq -r '.name'
# Ada

curl -s https://api.example.com/user | jq -r '.roles[0]'

Filter and map arrays

jq filters transform arrays inline — map fields, select by condition, and more.

bash
# names of active users
jq -r '.users[] | select(.active) | .name' data.json

# array of ids
jq '[.items[].id]' data.json

Pretty-print and validate

jq pretty-prints by default; use it to format or to check that a file is valid JSON.

bash
jq . data.json                 # pretty-print
jq empty data.json && echo OK  # exit 0 if valid JSON

Frequently Asked Questions

Use your package manager: apt install jq, brew install jq, or download a static binary. It's a single self-contained executable.

The -r (raw output) flag prints string results without surrounding quotes, which is essential when assigning values to shell variables.

Yes. Use the jq Playground to build and debug filters against your JSON before putting them in a script.

Related Tools