jq Mastery
jq is the SQL of JSON. Master it and you control every API, every config file, every data pipeline in your infrastructure.
Why jq Matters
Every modern tool outputs JSON:
-
APIs (REST, GraphQL) → JSON
-
CLIs (
kubectl,gh,glab,vault) → JSON -
Config files (package.json, terraform state) → JSON
-
Logs (structured logging) → JSON
Without jq, you’re stuck with grep and awk hacks. With jq, you have a real query language.
Learning Path
| Document | Focus | Skill Level |
|---|---|---|
Syntax, operators, patterns, yq for YAML |
Foundation |
|
Dependabot, GitLab vulns, CVE analysis |
Practical |
|
gh, glab, tea - repo management at scale |
Practical |
|
kubectl JSON mastery, resource analysis |
Intermediate |
|
ISE, pfSense, Vault, WLC via netapi |
Intermediate |
|
Pivoting, time series, set operations |
Advanced |
The Mental Model
Think of jq like SQL:
| SQL | jq | What It Does |
|---|---|---|
|
|
Iterate all items |
|
|
Filter |
|
|
Project fields |
|
|
Sort |
|
|
Group |
|
|
Count |
Quick Wins (Copy These Now)
Explore Unknown JSON
# What type is the root?
cat data.json | jq 'type'
# What keys exist?
cat data.json | jq 'keys'
# Keys with their types (THE pattern)
cat data.json | jq -r 'to_entries[] | "\(.key): \(.value | type)"'
Common Flags
| Flag | Purpose |
|---|---|
|
Raw output (no JSON quotes) |
|
Compact (one line per object) |
|
Slurp (read all inputs into array) |
|
Sort object keys |
|
Pass shell variable as string |
|
Pass shell variable as JSON |
The Three-Stage Pattern
Most infrastructure queries follow this:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Source │ --> │ Transform │ --> │ Present │
│ (API/CLI) │ │ (jq) │ │ (column/awk)│
└─────────────┘ └─────────────┘ └─────────────┘
# Source: Get raw data
netapi ise --format json get-policy-sets |
# Transform: Extract and shape
jq -r '.[] | [.name, .state, .rank] | @tsv' |
# Present: Human-readable
column -t -N "POLICY,STATE,RANK"
Next Steps
-
Start with Core Reference - understand the syntax
-
Apply to your daily tools - pick one deep-dive document
-
Practice with real API responses - don’t use toy examples
-
Build muscle memory - use jq daily for 2 weeks
|
The fastest way to learn jq: Save a real API response to a file, then iteratively build your query:
|
Resources
-
jq Manual - Official reference
-
jq Playground - Test queries interactively
-
fx JSON Explorer - Visual exploration before writing jq