gron - JSON 扁平化为可 Grep 格式
技能脚本入口
本技能附带两套等价包装脚本(Unix .sh + Windows PowerShell .ps1),提供友好子命令入口与安装自检,免去记忆原始参数。
macOS / Linux(bash):
bash scripts/gron-skill.sh <subcommand> [args...]
# 查看完整用法与示例
bash scripts/gron-skill.sh help
Windows(PowerShell):
.\scripts\gron-skill.ps1 <subcommand> [args...]
# 若被执行策略拦截,用下面这种调用绕过:
powershell -ExecutionPolicy Bypass -File scripts\gron-skill.ps1 <subcommand> [args...]
可用子命令: gron、ungron、grep、sed、diff、paths、values、remove、fzf
脚本选择规则: macOS / Linux 用 .sh;Windows 用 .ps1。两者子命令完全一致。
脚本仅作便捷封装,最终调用系统的
gron二进制;运行前请确保已安装对应工具(安装方式见下文各节)。说明:SkillHub 不支持.cmd/.bat文件类型,故不附带 Windows cmd 版;无 PowerShell 的环境可用 Git Bash 运行.sh版。个别依赖 Unix 管道工具的子命令(如 gron 的sed/fzf)在 Windows 下建议用 Git Bash 运行.sh版。
这是什么
gron (tomnomnom/gron, 8.7k ⭐) 将 JSON 转换为离散赋值语句 (路径=值),使 JSON 可用 grep、sed、awk 等行处理工具搜索、过滤、修改,最后再转回 JSON。核心理念:让 JSON 变成行,用熟悉的行工具处理。
- GitHub: https://github.com/tomnomnom/gron
- 文档: https://github.com/tomnomnom/gron#readme
- Go 编写,单二进制,零依赖
何时使用我
当用户出现以下意图时加载此技能:
- "gron 命令"、"json grep"、"grep json"
- "json 太深嵌套看不懂"、"展平 json"
- "对比两个 json 文件差异"、"json diff"
- "从 json 提取所有路径/键"
- "用 sed/awk 修改 json"
- "在管道中处理 json"、"json 转行格式"
- "查找 json 中所有某字段的值"
- "批量修改 json 中的特定值"
核心能力
1. JSON -> 扁平赋值 (gron)
# 基本转换
gron data.json
# 输出示例:
# json = {};
# json.users = [];
# json.users[0] = {};
# json.users[0].name = "Alice";
# json.users[0].age = 30;
# json.users[1] = {};
# json.users[1].name = "Bob";
# json.users[1].age = 25;
# 从 stdin
cat data.json | gron
# 保存为 .gron 文件
gron data.json > data.gron
2. 扁平赋值 -> JSON (gron --ungron / ungron)
# 转回 JSON
gron data.gron --ungron
# 或
ungron data.gron
# 从 stdin 转回
cat data.gron | ungron > data.json
3. 配合 grep 搜索
# 搜索所有包含 "error" 的值
gron logs.json | grep error
# 搜索特定路径
gron data.json | grep '\.name'
# 搜索特定值
gron data.json | grep '= "admin"'
# 正则搜索
gron data.json | grep -E '\.email.*@gmail\.com'
# 忽略大小写
gron data.json | grep -i 'error'
4. 配合 sed/awk 修改
# 批量替换值
gron data.json | sed 's/"old_value"/"new_value"/g' | ungron > fixed.json
# 删除特定路径
gron data.json | grep -v '\.password' | ungron > no_password.json
# 修改特定字段
gron data.json | sed 's/\.status = "active"/.status = "inactive"/' | ungron > updated.json
# 批量重命名键 (awk)
gron data.json | awk '{gsub(/old_key/, "new_key")} 1' | ungron > renamed.json
5. 提取路径与值
# 所有键路径
gron data.json | cut -d'=' -f1 | sort -u
# 所有叶子值
gron data.json | cut -d'=' -f2- | sort -u
# 特定前缀的路径
gron data.json | grep '^json\.users\['
# 数组索引路径
gron data.json | grep '\[0\]'
6. JSON Diff (对比两个 JSON)
# 语义对比 (忽略键序、格式)
diff <(gron file1.json | sort) <(gron file2.json | sort)
# 只显示差异值
diff -u <(gron file1.json | sort) <(gron file2.json | sort)
# 配合 colordiff 高亮
diff -u <(gron file1.json | sort) <(gron file2.json | sort) | colordiff
7. 流式处理大文件
# JSON Lines 逐行处理
cat huge.jsonl | while read line; do
echo "$line" | gron | grep 'error' | ungron
done
# 或用 jq 先拆分
jq -c '.[]' huge.json | while read obj; do
echo "$obj" | gron | grep 'important' | ungron
done
8. 与其他工具组合
# gron + fzf 交互式探索
gron data.json | fzf --preview 'echo {} | cut -d"=" -f2-'
# gron + jq (gron 搜索路径,jq 精确提取)
path=$(gron data.json | grep 'target_field' | head -1 | cut -d'=' -f1 | sed 's/^json//')
jq "$path" data.json
# gron + yq (跨格式)
gron data.json | grep 'config' | ungron | yq -P -o yaml
典型工作流示例
场景 1:调试复杂 API 响应
# 1. 保存响应
curl -s api.example.com/complex > response.json
# 2. 展平看结构
gron response.json | head -50
# 3. 搜索关键字段
gron response.json | grep -i token
gron response.json | grep '\.id'
# 4. 只看用户相关
gron response.json | grep '^json\.users\.' | ungron | jq .
场景 2:配置文件对比 (CI/CD)
# 对比两个环境的配置
diff -u \
<(gron config.prod.json | sort) \
<(gron config.staging.json | sort) \
| grep '^[-+]' | grep -v '^[-+]{3}' | head -20
场景 3:批量修改 JSON 配置
# 将所有 "http:" 改为 "https:"
gron config.json | sed 's/"http:"/"https:"/g' | ungron > config.json.new
mv config.json.new config.json
# 删除所有调试字段
gron config.json | grep -v '\.debug\|\.trace\|\.verbose' | ungron > config.clean.json
场景 4:提取所有唯一值做清单
# 提取所有 service 名称
gron microservices.json | grep '\.service =' | cut -d'"' -f2 | sort -u
# 提取所有端口
gron k8s.json | grep '\.port =' | cut -d'=' -f2 | sort -nu
场景 5:JSON Patch 生成
# 1. 对比生成差异路径
diff -u <(gron old.json | sort) <(gron new.json | sort) | \
grep '^[-+]json' | grep -v '^[-+]{3}' | \
sed 's/^[-+]//' > changes.txt
# 2. 手工审核后生成 patch
# 可用于自动化配置更新
高级用法
自定义基础对象名
# 默认基础对象名为 json
gron data.json # json = {};
# 自定义 (便于多文件合并)
gron --obj config data.json
# config = {};
# config.users = [];
处理数组索引
# gron 保留数组索引,便于定位
gron '{"items":[{"a":1},{"a":2}]}'
# json = {};
# json.items = [];
# json.items[0] = {};
# json.items[0].a = 1;
# json.items[1] = {};
# json.items[1].a = 2;
# 只看第一个元素
gron data.json | grep '\[0\]'
处理 null/布尔/数字
gron '{"a":null,"b":true,"c":123,"d":"str"}'
# json = {};
# json.a = null;
# json.b = true;
# json.c = 123;
# json.d = "str";
命令速查表
| 命令/参数 | 说明 |
|-----------|------|
| gron file.json | JSON -> 扁平赋值 |
| gron --ungron file.gron | 扁平赋值 -> JSON |
| ungron file.gron | 同 --ungron |
| gron --obj name | 自定义基础对象名 |
| gron --no-sort | 保持原序 (默认排序键) |
| gron --stream | 流式处理 JSON Lines |
安装方式
# macOS (Homebrew)
brew install gron
# Linux (二进制下载)
# GitHub Releases 下载对应架构
# Go 安装
go install github.com/tomnomnom/gron@latest
# Docker
docker run --rm -i ghcr.io/tomnomnom/gron < data.json
故障排查
| 问题 | 解决方案 |
|------|----------|
| "command not found" | go install github.com/tomnomnom/gron@latest |
| 输出太多 | 配合 head、grep、管道过滤 |
| ungron 报错 | 确保输入是合法 gron 格式 (每行以分号结尾) |
| 数组顺序乱 | gron 保留索引,ungron 会按索引还原 |
| 大文件内存 | gron 需加载整个 JSON,超大文件先用 jq 拆分 |
| 特殊字符转义 | gron 输出已转义,sed 处理时注意反斜杠 |
相关技能
- fx - 交互式 TUI JSON 查看器 (可视化探索)
- jq - 经典 JSON 处理器 (更强大的查询/变换)
- dasel - 统一选择器跨格式查询
- yq - YAML/JSON/XML/CSV 互转
- jless - 交互式 JSON 查看器 (类 less)
- sqlite-utils - 导入 SQLite 做 SQL 查询
技能版本: 1.0.0 | 作者: asdw741111 | 适用平台: OpenCode, Claude Code, Cursor, Codex CLI, Windsurf, Gemini CLI
Scan to join WeChat group