Communitygithub.com

Anhvu1107/all-agent-skill

在請求與 JQ 相關時一律使用此技能:用於 JSON 查詢、過濾、轉換和管線整合的專家級 jq 用法。

all-agent-skill 是什麼?

這個技能專為開發者與 AI 代理設計,提供 jq 命令列工具的專家級實用知識,涵蓋 JSON 查詢、過濾、轉換、聚合以及與 Shell 管線的整合。它支援任何可以使用 jq 的環境(如終端機、腳本、CI/CD),並提供大量可直接複製貼上的範例,從基本的欄位選取到進階的遞迴轉換與錯誤處理。技能文件強調最佳實踐,例如使用 `--arg` 安全地注入 Shell 變數、優先使用 `map(f)` 提升可讀性,並提醒常見陷阱(如大小寫差異、空陣列處理)。透過這份技能,代理能夠精準解析 API 回應、重塑 JSON 結構、在腳本中嵌入 jq 指令,或解釋複雜的 jq 表達式,從而大幅提升處理 JSON 資料的效率與可靠性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Anhvu1107/all-agent-skill/tree/HEAD/.agent/skills/jq

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

jq — JSON 查詢與轉換

選擇性閱讀規則

請從以下文件開始:

  • references/senior-master-standard.md
  • references/usage-routing.md
  • references/quality-checklist.md

然後僅載入與使用者實際任務相關的繼承文件、腳本、資源或範例。

概述

jq 是用於查詢和重塑 JSON 的標準 CLI 工具。此技能涵蓋實用、專家級的用法:過濾深層巢狀資料、轉換結構、聚合數值,以及將 jq 組合進 Shell 管線中。每個範例都可以直接複製貼上以用於實際工作流程。

何時使用此技能

  • 用於解析來自 API、CLI 工具(AWS、GitHub、kubectl、docker)或日誌檔案的 JSON 輸出時
  • 用於轉換 JSON 結構(重新命名鍵、扁平化陣列、分組記錄)時
  • 當使用者需要在 bash 腳本或單行程式中使用 jq 時
  • 當需要解釋複雜的 jq 表達式時

運作方式

jq 接受一個過濾器表達式,並將其應用於 JSON 輸入。過濾器透過管線符號 (|) 組合,jq 原生支援陣列、物件、字串、數字、布林值和 null。

基本選取

# 提取一個欄位
echo '{"name":"alice","age":30}' | jq '.name'
# "alice"

# 巢狀存取
echo '{"user":{"email":"[email protected]"}}' | jq '.user.email'

# 陣列索引
echo '[10, 20, 30]' | jq '.[1]'
# 20

# 陣列切片
echo '[1,2,3,4,5]' | jq '.[2:4]'
# [3, 4]

# 所有陣列元素
echo '[{"id":1},{"id":2}]' | jq '.[]'

使用 select 過濾

# 僅保留符合條件的元素
echo '[{"role":"admin"},{"role":"user"},{"role":"admin"}]' \
  | jq '[.[] | select(.role == "admin")]'

# 數值比較
curl -s https://api.github.com/repos/owner/repo/issues \
  | jq '[.[] | select(.comments > 5)]'

# 測試欄位是否存在且非 null
jq '[.[] | select(.email != null)]'

# 組合條件
jq '[.[] | select(.active == true and .score >= 80)]'

映射與轉換

# 從每個陣列元素提取一個欄位
echo '[{"name":"alice","age":30},{"name":"bob","age":25}]' \
  | jq '[.[] | .name]'
# ["alice", "bob"]

# 縮寫:map()
jq 'map(.name)'

# 為每個元素建立新物件
jq '[.[] | {user: .name, years: .age}]'

# 新增一個計算後的欄位
jq '[.[] | . + {senior: (.age > 28)}]'

# 重新命名鍵
jq '[.[] | {username: .name, email_address: .email}]'

聚合與 Reduce

# 加總所有值
echo '[1, 2, 3, 4, 5]' | jq 'add'
# 15

# 計算物件中某個欄位的總和
jq '[.[].price] | add'

# 計算元素數量
jq 'length'

# 最大值 / 最小值
jq 'max_by(.score)'
jq 'min_by(.created_at)'

# reduce:自訂累積器
echo '[1,2,3,4,5]' | jq 'reduce .[] as $x (0; . + $x)'
# 15

# 按欄位分組
jq 'group_by(.department)'

# 依分組計數
jq 'group_by(.status) | map({status: .[0].status, count: length})'

字串插值與格式化

# 字串插值
jq -r '.[] | "\(.name) 是 \(.age) 歲"'

# 格式化為 CSV(無標題)
jq -r '.[] | [.name, .age, .email] | @csv'

# 格式化為 TSV
jq -r '.[] | [.name, .score] | @tsv'

# URL 編碼一個值
jq -r '.query | @uri'

# Base64 編碼
jq -r '.data | @base64'

處理鍵與路徑

# 列出所有頂層鍵
jq 'keys'

# 檢查鍵是否存在
jq 'has("email")'

# 刪除一個鍵
jq 'del(.password)'

# 從每個元素刪除巢狀鍵
jq '[.[] | del(.internal_id, .raw_payload)]'

# 遞迴下降:在樹中的任何位置尋找某個鍵的所有值
jq '.. | .id? // empty'

# 取得所有葉子路徑
jq '[paths(scalars)]'

條件判斷與錯誤處理

# if-then-else
jq 'if .score >= 90 then "A" elif .score >= 80 then "B" else "C" end'

# 替代運算子:如果為 null 或 false 則使用備用值
jq '.nickname // .name'

# try-catch:跳過錯誤而非停止
jq '[.[] | try .nested.value catch null]'

# 使用 // empty 抑制 null 輸出
jq '.[] | .optional_field // empty'

實用的 Shell 整合

# 從檔案讀取
jq '.users' data.json

# 緊湊輸出(無空白)以便進一步管道處理
jq -c '.[]' records.json | while IFS= read -r record; do
  echo "正在處理:$record"
done

# 將 Shell 變數傳入 jq
STATUS="active"
jq --arg s "$STATUS" '[.[] | select(.status == $s)]'

# 傳入數字
jq --argjson threshold 42 '[.[] | select(.value > $threshold)]'

# 將多個 JSON 行吞入一個陣列
jq -s '.' records.ndjson

# 多個檔案:將所有檔案吞入一個陣列
jq -s 'add' file1.json file2.json

# 從命令安全地進行管線處理
kubectl get pods -o json | jq '.items[] | {name: .metadata.name, status: .status.phase}'

# GitHub CLI:提取 PR 編號
gh pr list --json number,title | jq -r '.[] | "\(.number)\t\(.title)"'

# AWS CLI:列出執行中的執行個體 ID
aws ec2 describe-instances \
  | jq -r '.Reservations[].Instances[] | select(.State.Name=="running") | .InstanceId'

# Docker:顯示容器名稱和映像檔
docker inspect $(docker ps -q) | jq -r '.[] | "\(.Name)\t\(.Config.Image)"'

進階模式

# 將物件陣列轉置為物件陣列
# 輸入:{"names":["a","b"],"scores":[10,20]}
jq '[.names, .scores] | transpose | map({name: .[0], score: .[1]})'

# 展開一層
jq 'flatten(1)'

# 依欄位去重
jq 'unique_by(.email)'

# 排序、去重並重新索引
jq '[.[] | .name] | unique | sort'

# walk:遞迴地對每個節點套用轉換
jq 'walk(if type == "string" then ascii_downcase else . end)'

# env:在 jq 內部讀取環境變數
export API_KEY=secret
jq -n 'env.API_KEY'

最佳實踐

  • 當將 jq 結果傳遞給 Shell 變數或其他命令時,務必使用 -r(原始輸出)以去除 JSON 字串的引號
  • 使用 --arg / --argjson 安全地注入 Shell 變數——絕不要直接在過濾器字串中插值 Shell 變數
  • 為了可讀性,優先使用 map(f) 而非 [.[] | f]
  • 對於換行分隔的 JSON 管線,使用 -c(緊湊);為了人類可讀的除錯則省略它
  • 在嵌入腳本之前,先用 jq -n 和字面輸入互動式測試過濾器
  • 使用 empty 來丟棄不需要的元素,而非過濾為 null

安全性與注意事項

  • jq 預設為唯讀——它無法寫入檔案或執行命令
  • 避免將不受信任的 JSON 欄位值直接嵌入 Shell 命令;務必使用引號或 --arg

常見陷阱

  • 問題: jq 輸出 null 而非預期值 解決方案: 檢查鍵名稱是否有拼寫錯誤;使用 keys 檢查實際欄位名稱。請記住 JSON 區分大小寫。

  • 問題: 輸出中的數字被用引號括起來成為字串 解決方案: 注入數值時使用 --argjson 而非 --arg。

  • 問題: 過濾器在終端機中有效,但在腳本中失敗 解決方案: 確保過濾器字串在 Shell 中使用單引號以防止變數展開。範例:使用 jq '.field' 而非 jq ".field"。

  • 問題: add 在空陣列上返回 null 解決方案: 使用 add // 0 或 add // "" 來提供備用預設值。

  • 問題: 串流處理大型檔案很慢 解決方案: 對於非常大的檔案,使用 jq --stream 或改用 jstream/gron。

相關技能

  • @bash-pro — 將 jq 呼叫包裝在穩健的 Shell 腳本中
  • @bash-linux — 一般 Shell 管道模式
  • @github-automation — 將 jq 與 GitHub CLI JSON 輸出一起使用

限制

  • 僅在任務明確符合上述範圍時使用此技能。
  • 不要將輸出視為環境特定驗證、測試或專家審查的替代品。
  • 如果缺少所需的輸入、權限、安全邊界或成功標準,請停下來並要求澄清。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches 007: Security audit, hardening, threat modeling (STRIDE/PASTA), Red/Blue Team, OWASP checks, code review, incident response, and infrastructure security for any project.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Agentfolio: Skill for discovering and researching autonomous AI agents, tools, and ecosystems using the AgentFolio directory.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Animejs Animation: Advanced JavaScript animation library skill for creating complex, high-performance web animations.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Astropy: Astropy is the core Python package for astronomy, providing essential functionality for astronomical research and data analysis.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Astro: Build content-focused websites with Astro — zero JS by default, islands architecture, multi-framework components, and Markdown/MDX support.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Emotional ARC Designer: One sentence - what this skill does and when to invoke it

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Fixing Motion Performance: Audit and fix animation performance issues including layout thrashing, compositor properties, scroll-linked motion, and blur effects.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches I18n Localization: Internationalization and localization patterns.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Interactive Portfolio: Expert in building portfolios that actually land jobs and clients - not just showing work, but creating memorable experiences.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Landing Page Generator: Generates high-converting Next.js/React landing pages with Tailwind CSS.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches LEX: Centralized ''Truth Engine'' for cross-jurisdictional legal context (US, EU, CA) and contract scaffolding.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Magic Animator: AI-powered animation tool for creating motion in logos, UI, icons, and social media assets.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Makepad Animation: CRITICAL: Use for Makepad animation system.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Nextjs APP Router Patterns: Comprehensive patterns for Next.js 14+ App Router architecture, Server Components, and modern full-stack React development.

Anhvu1107/all-agent-skill

ALWAYS use this when the user mentions Nextjs Best Practices, asks to build, debug, review, document, automate, test, configure, migrate, or make decisions in this domain, or the task clearly depends on Nextjs Best Practices; scope: Next.js App Router principles. Apply the bundled workflow, references, scripts, Senior Master standard, and Codex strict review gate before final output.

Anhvu1107/all-agent-skill

description: ALWAYS use this when the work is specifically about modern Next.js or React architecture, data flow, rendering strategy, routing, or performance-sensitive frontend behavior.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches Nextjs Supabase Auth: Expert integration of Supabase Auth with Next.js App Router

Anhvu1107/all-agent-skill

ALWAYS use this when the task involves reading, extracting, splitting, merging, rotating, creating, validating, OCR-ing, form-filling, or otherwise producing or modifying PDF files.

Anhvu1107/all-agent-skill

ALWAYS use this when the request matches React Nextjs Development: React and Next.js 14+ application development with App Router, Server Components, TypeScript, Tailwind CSS, and modern frontend patterns.

Anhvu1107/all-agent-skill

ALWAYS use this when working with Remotion code, video compositions, rendering logic, timing, media assets, or React-based motion workflows.

相關技能