jq¶
- Website / Docs
- Github
- jqplay.org: A playground for jq
- Alternative jq implementations
Frontends to jq¶
ijq¶
jqp¶
- GitHub
- A TUI playground to experiment with jq
- Install:
pamac install jqp-bin
Install¶
- Arch:
sudo pacman -S jq - Debian:
sudo apt install jq
Usage¶
Parse / extract data¶
Suppress quotes from output (raw output):
List all sha:
List all keys:
https://michaelheap.com/extract-keys-using-jq/
List all extension data:
export JSON=~/.mozilla/firefox/default/addons.json
jq '.addons[].id' < $JSON
jq '.addons[] | { name: .name, id: .id, type: .type }' < $JSON
grep 'extensions.xpiState' ~/.mozilla/firefox/default/prefs.js | \
sed 's/^user_pref("extensions.xpiState", "//' | sed 's/");$//' | \
sed 's/\\//g' | jq .
Special characters (i.e. dash) in keys:
echo '{"p-name": "Dr Nic"}' | jq -r '.["p-name"]'
curl -s --netrc echo.oas.varac.net -L | jq '.request.headers["x-real-ip"]'
Output as table¶
Use @csv (or @tsv) output option to first convert to a table,
then use tools like csv2md to produce a markdown table
(see etc/tables.md for other tools):
Tip: wrapping long values in tables can be done with i.e. glow
Filter objects based on the contents of a value¶
jq '.letsencrypt.Certificates[].domain | select(.main | contains("www"))' < letsencrypt/acme.json
ip -j route list | jq '.[] | select(.protocol=="unspec")'
Filter all results which have not the status of IGNORED or PASSED:
Filter and remove key containing a value:
jq '.| del(.letsencrypt.Certificates[] | select(.domain.main == "example.org"))' letsencrypt/acme.json | \
sponge letsencrypt/acme.json`
Modify data¶
jq '.Statement[0].Resource = ["arn:aws:s3:::restic-zancas"]' \
~/projects/cloud/storage/minio/policy.readwrite.json
Examples¶
jq with prometheus¶
Alert count:
Successful ?
Filter out the none serverity alerts:
jq -c '.data.alerts[] | select(.labels.severity | contains("none") | not)' < example-alerts.json | jq .
and count them:
jq -c '[.data.alerts[] | select(.labels.severity | contains("none") | not )] | length' < example-alerts.json
ndjson¶
jq can parse
ndjson
Compact lines (but wrap):