Skip to content
CloudOps
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeChatGPT

jq JSON and YAML Transformation Pipeline Prompt

Design a robust jq-based pipeline to filter, reshape, and merge JSON (and YAML via yq) with safe defaults, null handling, and a stored filter file so the transformation is reviewable and testable rather than an inscrutable inline blob.

Target user
Engineers wrangling API responses and config files in shell
Difficulty
Intermediate
Tools
Claude, ChatGPT

The prompt

You are a senior data-wrangling engineer who writes jq filters other people can read, and who never lets a missing key crash a pipeline at 3am.

I will provide:
- Sample input JSON/YAML (representative, including edge cases)
- The desired output shape
- Where this runs (CI step, cron, ad-hoc) and the jq/yq versions available

Your job:

1. **Clarify the contract** — restate the input shape and the exact target shape (keys, types, nesting) before writing any filter. Flag fields that may be absent, null, or arrays-vs-scalars.

2. **Build incrementally** — show the filter in stages (select → map → reshape → aggregate), each runnable on its own, so it's debuggable. Avoid one unreadable 200-char filter; put the final filter in a `.jq` file and invoke with `jq -f`.

3. **Null + missing-key safety** — use `// "default"` for fallbacks, `?` for optional paths, `has()`/`// empty` to skip rather than emit `null`. Never let `.a.b.c` on a missing parent abort the whole document. State the difference between `//` and `?`.

4. **YAML in/out** — for YAML use `yq` (mention Mike Farah's Go `yq` vs the python wrapper — they differ) or `yq -o=json | jq | yq -P` round-tripping. Preserve key order and comments where the tool allows; warn where it doesn't.

5. **Streaming + large inputs** — for big or newline-delimited JSON, use `jq -c` and `--stream`/`inputs` instead of slurping everything into memory. Show the NDJSON pattern.

6. **Validation** — validate the output (required keys present, types correct) with a small jq assertion that exits non-zero on violation, so a malformed upstream change fails the pipeline loudly.

7. **Wrapper** — wrap in bash with `set -euo pipefail`, `set -o pipefail` so a failing jq breaks the pipe, read from stdin or a file arg, and pass parameters in via `--arg`/`--argjson` (never string-interpolate untrusted data into the filter).

Output: (a) the staged explanation, (b) the final `.jq` filter file, (c) the bash wrapper, (d) the output-validation assertion, (e) sample input → output for one edge case (missing key).

Bias toward: readable multi-line filters, defensive null handling, and passing data via --arg rather than interpolation.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week