jq Session 02: Filters

Building on fundamentals. This session covers the pipe operator, conditional selection, mapping over arrays, and extracting keys/values.

Pre-Session State

  • Can access object properties with .key

  • Can access array elements with .[n] and .[]

  • Understand -r for raw output

Setup

cat > /tmp/jq-hosts.json << 'EOF'
[
  {"name": "kvm-01", "ip": "10.50.1.110", "role": "hypervisor", "active": true},
  {"name": "kvm-02", "ip": "10.50.1.111", "role": "hypervisor", "active": true},
  {"name": "vault-01", "ip": "10.50.1.60", "role": "secrets", "active": true},
  {"name": "vault-02", "ip": "10.50.1.61", "role": "secrets", "active": false},
  {"name": "ise-01", "ip": "10.50.1.20", "role": "nac", "active": true}
]
EOF

Lesson 1: Pipe Operator

Concept: The | chains filters together, passing output to input.

Exercise 1.1: Chain access

cat /tmp/jq-hosts.json | jq '.[0] | .name'

Output: "kvm-01"

Exercise 1.2: Multiple pipes

cat /tmp/jq-hosts.json | jq '.[] | .name | ascii_upcase'

Output: All names in uppercase.

Lesson 2: Select Filter

Concept: select(condition) keeps only items matching the condition.

Exercise 2.1: Filter by role

cat /tmp/jq-hosts.json | jq '.[] | select(.role == "hypervisor")'

Output: Only kvm-01 and kvm-02 objects.

Exercise 2.2: Filter by boolean

cat /tmp/jq-hosts.json | jq '.[] | select(.active == false) | .name'

Output: "vault-02"

Exercise 2.3: Filter with contains

cat /tmp/jq-hosts.json | jq '.[] | select(.name | contains("vault")) | .ip'

Output: vault IP addresses.

Lesson 3: Map Function

Concept: map(filter) applies a filter to each array element.

Exercise 3.1: Extract all names

cat /tmp/jq-hosts.json | jq 'map(.name)'

Output: ["kvm-01", "kvm-02", "vault-01", "vault-02", "ise-01"]

Exercise 3.2: Map with transformation

cat /tmp/jq-hosts.json | jq 'map(.name + " - " + .role)'

Output: Array of "name - role" strings.

Lesson 4: Keys and Values

Concept: Extract object keys or values as arrays.

Exercise 4.1: Get object keys

cat /tmp/jq-hosts.json | jq '.[0] | keys'

Output: ["active", "ip", "name", "role"]

Exercise 4.2: Get object values

cat /tmp/jq-hosts.json | jq '.[0] | to_entries'

Output: Array of {"key": "…​", "value": "…​"} objects.

Exercise 4.3: Length

cat /tmp/jq-hosts.json | jq 'length'
cat /tmp/jq-hosts.json | jq '.[0].name | length'

Output: 5 (array length), 6 (string length of "kvm-01")

Summary: What You Learned

Concept Syntax Example

Pipe

|

.[] | .name chain filters

Select

select(cond)

select(.active == true)

Map

map(filter)

map(.name) extract field

Keys

keys

.[0] | keys list fields

Length

length

length count items

Contains

contains(str)

select(.name | contains("vault"))

Exercises to Complete

  1. [ ] Get names of all active hosts

  2. [ ] Count how many hosts have role "secrets"

  3. [ ] Get IP addresses of hypervisors only

  4. [ ] Create array of "name: ip" strings

Next Session

Session 03: Construction - Building new objects and arrays, @csv output.

Session Log

Timestamp Notes

Start

<Record when you started>

End

<Record when you finished>