gron Session 01: Basics
gron transforms JSON into discrete assignments. Each line is a complete path to a value. This makes JSON greppable — the whole point of the tool.
Pre-Session State
-
Can read JSON structure
-
Know basic
grepusage -
Have
groninstalled (sudo pacman -S gron)
Setup
cat > /tmp/gron-01.json << 'EOF'
{
"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
Lesson 1: Flatten JSON
Concept: gron converts JSON into JavaScript-style assignment statements. Each line is a complete path.
Exercise 1.1: Flatten the file
gron /tmp/gron-01.json
Output: Every value becomes its own line:
json = {};
json.hostname = "ise-01";
json.ip = "10.50.1.20";
json.config = {};
json.config.tls = {};
json.config.tls.enabled = true;
json.config.tls.port = 443;
...
Each line is self-contained. That’s the power — you can grep any of them.
Lesson 2: Grep the Paths
Concept: Once flattened, standard grep finds what you need.
Exercise 2.1: Find TLS-related fields
gron /tmp/gron-01.json | grep tls
Output: Only lines containing "tls" — complete paths to every TLS-related value.
Exercise 2.2: Find all IPs
gron /tmp/gron-01.json | grep -i ip
Exercise 2.3: Find array elements
gron /tmp/gron-01.json | grep 'interfaces\['
Output: All interface entries with their full paths.
Exercise 2.4: Find specific values
gron /tmp/gron-01.json | grep '= 443'
Output: json.config.tls.port = 443; — now you know the exact path.
Lesson 3: ungron — Reconstruct JSON
Concept: gron --ungron (or ungron) reverses the flattening. Filter lines with grep, then reconstruct valid JSON from the subset.
Exercise 3.1: Round-trip (identity)
gron /tmp/gron-01.json | gron --ungron
Output: Original JSON reconstructed. gron | ungron is the identity operation.
Exercise 3.2: Filter and reconstruct
gron /tmp/gron-01.json | grep config | gron --ungron
Output: A valid JSON document containing only the config subtree.
Exercise 3.3: Extract interfaces only
gron /tmp/gron-01.json | grep interfaces | gron --ungron
Output: Valid JSON with just the interfaces array.
Lesson 4: The Discovery Workflow
Concept: gron → grep → discover path → use jq. This is the workflow.
Exercise 4.1: Discover, then extract
# Step 1: What paths mention "cert"?
gron /tmp/gron-01.json | grep cert
# Output: json.config.tls.cert_path = "/etc/ssl/certs/ise.pem";
# Step 2: Now use jq with the known path
jq '.config.tls.cert_path' /tmp/gron-01.json
The gron path json.config.tls.cert_path maps directly to jq’s .config.tls.cert_path — just drop the json prefix.
Exercise 4.2: Discover nested structure
# What's inside config?
gron /tmp/gron-01.json | grep '^json\.config\.' | head -10
Shows every leaf under config with full paths.
Lesson 5: Flags
Exercise 5.1: Colorized output
gron --color /tmp/gron-01.json | head -10
Exercise 5.2: Stream mode
echo '{"a":1}{"b":2}' | gron -s
-s handles streaming JSON (newline-delimited objects).
Summary: What You Learned
| Concept | Syntax | Example |
|---|---|---|
Flatten |
|
Lines of |
Search |
|
|
Reconstruct |
|
Filter to valid JSON subset |
Round-trip |
|
Identity operation |
Discovery |
gron → grep → jq |
Find path, then extract |
Exercises to Complete
-
[ ] Find all fields containing "10.50" in the test data
-
[ ] Extract only the TLS config as valid JSON
-
[ ] Find the cert_path using gron, then extract it with jq
-
[ ] Flatten, remove all array entries, reconstruct (what remains?)
Next Session
Session 02: API Discovery - Using gron to explore GitHub and other APIs.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |