Drill 01: Basics

Flatten JSON, grep paths, reconstruct with ungron. The core workflow.

Run This Drill

bash ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/examples/gron-drills/01-basics.sh

Drill Script

#!/bin/bash
# GRON DRILL 01: BASICS
# Paste this entire script into your terminal
# Topics: Flatten, grep paths, ungron round-trip

cat << 'EOF' > /tmp/gron-01.json
{
  "hostname": "ise-01",
  "ip": "10.50.1.20",
  "domain": "inside.domusdigitalis.dev",
  "roles": ["pan", "mnt", "psn"],
  "config": {
    "version": "3.3",
    "patches": ["patch1", "patch2"],
    "tls": {
      "enabled": true,
      "port": 443,
      "cert_path": "/etc/ssl/certs/ise.pem"
    }
  },
  "interfaces": [
    {"name": "eth0", "ip": "10.50.1.20", "vlan": 50},
    {"name": "eth1", "ip": "10.50.10.20", "vlan": 10}
  ]
}
EOF

echo "=================================================================="
echo "             GRON DRILL 01: BASICS                                "
echo "=================================================================="
echo ""
echo "Test file: /tmp/gron-01.json"
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.1: FLATTEN JSON"
echo "Every value becomes a self-contained assignment line"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json"
gron /tmp/gron-01.json
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.2: GREP FOR PATHS"
echo "Find fields by name — the whole point of gron"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json | grep tls"
gron /tmp/gron-01.json | grep tls
echo ""
echo "Command: gron /tmp/gron-01.json | grep -i ip"
gron /tmp/gron-01.json | grep -i ip
echo ""
echo "Command: gron /tmp/gron-01.json | grep 'interfaces\['"
gron /tmp/gron-01.json | grep 'interfaces\['
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.3: FIND BY VALUE"
echo "Search for specific values, not just keys"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json | grep '= 443'"
gron /tmp/gron-01.json | grep '= 443'
echo ""
echo "Command: gron /tmp/gron-01.json | grep '= true'"
gron /tmp/gron-01.json | grep '= true'
echo ""
echo "(Now you know the path: json.config.tls.port and json.config.tls.enabled)"
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.4: UNGRON — ROUND-TRIP"
echo "gron | ungron = identity (reconstructs JSON)"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json | gron --ungron"
gron /tmp/gron-01.json | gron --ungron
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.5: FILTER AND RECONSTRUCT"
echo "grep removes lines, ungron builds valid JSON from what's left"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json | grep config | gron --ungron"
echo "(Only the config subtree as valid JSON)"
gron /tmp/gron-01.json | grep config | gron --ungron
echo ""
echo "Command: gron /tmp/gron-01.json | grep interfaces | gron --ungron"
echo "(Only interfaces as valid JSON)"
gron /tmp/gron-01.json | grep interfaces | gron --ungron
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.6: DISCOVERY → EXTRACTION"
echo "gron finds the path, jq extracts the value"
echo "------------------------------------------------------------------"
echo ""
echo "Step 1 — gron discovers:"
echo "Command: gron /tmp/gron-01.json | grep cert"
gron /tmp/gron-01.json | grep cert
echo ""
echo "Step 2 — jq extracts (drop 'json' prefix):"
echo "Command: jq '.config.tls.cert_path' /tmp/gron-01.json"
jq '.config.tls.cert_path' /tmp/gron-01.json
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.7: EXPLORE DEPTH"
echo "Count nesting levels to understand document shape"
echo "------------------------------------------------------------------"
echo ""
echo "Command: gron /tmp/gron-01.json | awk -F'.' '{print NF}' | sort -n | uniq -c | sort -rn"
gron /tmp/gron-01.json | awk -F'.' '{print NF}' | sort -n | uniq -c | sort -rn
echo ""
echo "(Left: count of fields. Right: nesting depth.)"
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "YOUR TURN - TRY THESE:"
echo "------------------------------------------------------------------"
echo ""
echo "1. Find all fields containing '10.50':"
echo "   gron /tmp/gron-01.json | grep '10\.50'"
echo ""
echo "2. Extract only TLS config as JSON:"
echo "   gron /tmp/gron-01.json | grep tls | gron --ungron"
echo ""
echo "3. Find roles (discover), then use jq:"
echo "   gron /tmp/gron-01.json | grep roles"
echo "   jq '.roles[]' /tmp/gron-01.json"
echo ""
echo "------------------------------------------------------------------"
echo "KEY TAKEAWAYS:"
echo "1. gron flattens JSON into greppable lines"
echo "2. Each line is json.path.to.value = value;"
echo "3. grep filters lines, ungron reconstructs JSON"
echo "4. gron path → drop 'json' prefix → jq path"
echo "5. Use gron for discovery, jq for extraction"
echo "------------------------------------------------------------------"