jq Session 01: Fundamentals

Starting from zero. This session covers the identity filter, object access, array indexing, and understanding JSON types.

Pre-Session State

  • Can read JSON files

  • Understand JSON structure (objects, arrays, strings, numbers)

  • Know basic shell piping

Setup

  1. Create test data file:

cat > /tmp/jq-test.json << 'EOF'
{
  "name": "modestus-razer",
  "ip": "10.50.10.111",
  "vlan": 10,
  "active": true,
  "tags": ["workstation", "linux", "admin"],
  "ports": [22, 80, 443]
}
EOF

Lesson 1: Identity Filter

Concept: The . (dot) is the identity filter - it outputs the input unchanged.

Exercise 1.1: Pretty print JSON

cat /tmp/jq-test.json | jq '.'

What happens: JSON is pretty-printed with colors and indentation.

Exercise 1.2: Compact output

cat /tmp/jq-test.json | jq -c '.'

What happens: JSON is output on a single line (compact).

Lesson 2: Object Access

Concept: Use .key to access object properties.

Exercise 2.1: Get a string value

cat /tmp/jq-test.json | jq '.name'

Output: "modestus-razer" (with quotes - it’s a JSON string)

Exercise 2.2: Raw output (-r flag)

cat /tmp/jq-test.json | jq -r '.name'

Output: modestus-razer (no quotes - raw string)

Key insight: Use -r when you need the value for shell scripts.

Exercise 2.3: Get a number

cat /tmp/jq-test.json | jq '.vlan'

Output: 10 (no quotes - it’s a number)

Lesson 3: Array Access

Concept: Use .[n] for array index, .[] for all elements.

Exercise 3.1: First array element

cat /tmp/jq-test.json | jq '.tags[0]'

Output: "workstation"

Exercise 3.2: All array elements

cat /tmp/jq-test.json | jq '.tags[]'

Output: Each tag on its own line.

Exercise 3.3: Array slice

cat /tmp/jq-test.json | jq '.ports[0:2]'

Output: [22, 80] (first two elements)

Lesson 4: Type Checking

Concept: Use type to see what kind of value you have.

Exercise 4.1: Check types

cat /tmp/jq-test.json | jq '.name | type'
cat /tmp/jq-test.json | jq '.vlan | type'
cat /tmp/jq-test.json | jq '.active | type'
cat /tmp/jq-test.json | jq '.tags | type'

Output: "string", "number", "boolean", "array"

Summary: What You Learned

Concept Syntax Example

Identity

.

jq '.' pretty prints

Object access

.key

.name gets name field

Raw output

-r

jq -r '.name' no quotes

Array index

.[n]

.tags[0] first element

All elements

.[]

.tags[] iterate array

Type check

type

.name | type returns "string"

Exercises to Complete

  1. [ ] Get the IP address as raw string

  2. [ ] Get all ports as separate lines

  3. [ ] Check if active is true using .active

  4. [ ] Get the second tag

Post-Session Reflection

What clicked:

  • <Write what made sense>

What’s still fuzzy:

  • <Write what needs more practice>

Connection to work:

  • <How will you use this?>

CLI Application

# Parse netapi output
netapi ise -f json mnt sessions | jq '.[0].nas_ip_address'

# Parse kubectl output
kubectl get pods -o json | jq '.items[0].metadata.name'

Next Session

Session 02: Filters - Pipe operator, select, map, keys, values.

Session Log

Timestamp Notes

Start

<Record when you started>

End

<Record when you finished>

Duration

<Actual time spent>