工具与资源github.com
Gron:让 JSON 变得可 grep
Gron – Make JSON Greppable
2018 — 2020,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
gron 是一个把 JSON 转换成扁平赋值语句的命令行工具,让 grep、sed 等已有工具可以直接搜索,并可通过 gron -u 还原。它主要解决在未知结构的深层 JSON 中定位字段路径的问题,而不是取代更强大的 jq。
为什么经久不衰:它用极简的扁平化输出降低了探索未知 JSON 结构的门槛,还能规避部分工具处理大数时的精度问题,今天仍是实用的 Unix 风格小工具。
HN 评论精选
gron 的定位是配合 grep/sed 探索未知结构,jq 更强大但学习和使用门槛更高。
来自 FAQ,免得有人问那个显而易见的问题:为什么我不直接用 jq?jq 很棒,也比 gron 强大得多,但强大也带来复杂性。gron 的目标是让你更容易使用已经熟悉的工具,比如 grep 和 sed。gron 的主要用途是:当你还不知道深层嵌套 JSON 的结构时,帮你轻松找到某个值所在的路径;jq 的很多能力只有在你已经了解结构之后才能发挥出来。
theshrike79查看英文原文
From the FAQ, before someone asks the obvious: Why shouldn't I just use jq? jq is awesome, and a lot more powerful than gron, but with that power comes complexity. gron aims to make it easier to use the tools you already know, like grep and sed. gron's primary purpose is to make it easy to find the path to a value in a deeply nested JSON blob when you don't already know the structure; much of jq's power is unlocked only once you know that structure.
gron 能保留大整数精度,而 jq 存在大数被改写的已知问题。
我喜欢它处理大数时不会像 jq 那样改动数值。比如 echo '{"a": 13911860366432393}' | jq '.' 会输出 ...392,而用 gron | gron -u 能保留 ...393。我现在可以愉快地卸载 jq 了,我被它坑过太多次。
vhanda查看英文原文
I love that this handles big numbers without modifying them unlike `jq` - https://github.com/stedolan/jq/issues/2182 $ echo "{\"a\": 13911860366432393}" | jq "." { "a": 13911860366432392 } $ echo "{\"a\": 13911860366432393}" | gron | gron -u { "a": 13911860366432393 } I can now happily uninstall `jq`. I've been burned by it way too many times.
值得系统学习 jq,因为结构化数据处理是日常刚需,jq 能精确查询而非依赖 grep 误报。
我认为你应该花大约一小时学一下 jq。是的,它远不是编程语言设计的典范,但它覆盖了所有边界情况,学会之后效率很高。(不过,“每年需要时从 StackOverflow 复制粘贴一条 one-liner”的策略行不通。)结构化数据现在太常见了,你必须投入学习处理它的工具。我个人投入过一次,它每天都在帮我。以前遇到“这个 Pod 监听哪个端口”这类问题,我会写 kubectl get pod foo -o yaml | grep port -A 3,通常要手动排除误报才能得到答案;用 jq 可以直接得到正确结果:kubectl get pod foo -o json | jq '.spec.containers[].ports'。也许有点难懂,但我保证值得。
jrockway查看英文原文
I think you should just invest the hour or so it takes to learn jq. Yes, it's far from a programming language design marvel. But it covers all of the edge cases, and once you learn it, you can be very productive. (But, the strategy of "copy paste a oneliner from Stackoverflow the one time a year I need it" isn't going to work.) I think structured data is so common now, that you have to invest in learning tools for processing it. Personally, I invested the time once, and it saves me every single day. In the past, I would have a question like "which port is this Pod listening on", and write something like "kubectl get pod foo -o yaml | grep port -A 3". Usually you get your answer after manually reading through the false-positives. But with "jq", you can just drive directly to the correct answer: "kubectl get pod foo -o json | jq '.spec.containers[].ports'" Maybe it's kind of obtuse, but it's worth your time, I promise.
批评 gron 输出中的括号和点仍需转义,不如用下划线分隔的键名更利于 grep 和 shell eval。
我完全不觉得这个好 grep:json[0].commit.author.name = "Tom Hudson"; 现在我得在正则里转义方括号和点。天才!我有一个 5 行的 jq 脚本能生成:json_0_commit_author_name='Tom Hudson'。这才是我说的可 grep,也可 eval。> 如果有 json 对象同时包含 commit 和 json_commit 怎么办?那我会用 jq 适当过滤,或改分隔符。重点是方便 grep 和 shell 使用。
inshadows查看英文原文
I don't find this grep-able at all: json[0].commit.author.name = "Tom Hudson"; Now I need to escape brackets and dots in regex. Genius! I have 5 line (!) jq script that produces this: json_0_commit_author_name='Tom Hudson' This is what I call grep-able. It's also eval-able. > What if there's json object with commit and json_commit? Then I'll use jq to filter it appropriately or change delimiter. The point is ease of use for grep and shell.
历次讨论
同类还值得读
直接在浏览器中启动 Linux 内核
Boot a linux kernel right inside your browser.
Fabrice Bellard 的 JSLinux 页面,用 JavaScript 在浏览器里启动一个 Linux 内核,并提供一个可交互的终端环境。
工具与资源上首页 5 次 · 2011–2025bellard.orgHacker News 未公开功能与行为清单(2018)
A List of Hacker News's Undocumented Features and Behaviors (2018)
数据科学家 Max Woolf 整理的 Hacker News 未公开功能与行为清单,涵盖投票机制、排名算法、账号惩罚等社区运作细节。
工具与资源上首页 5 次 · 2019–2025github.comMapSCII:在终端中用盲文和 ASCII 渲染地图
MapSCII – Braille and ASCII map renderer for the console
MapSCII 是一个在命令行终端里渲染地图的开源工具,用盲文字符和 ASCII 字符把地理数据画成可交互浏览的字符地图。
工具与资源上首页 5 次 · 2017–2025github.com顶尖大学的免费在线课程
Free Online Courses from Top Universities
Open Culture 整理的一份免费在线课程合集,汇集了全球顶尖大学(如 MIT、斯坦福、耶鲁等)向公众开放的网络课程资源,涵盖音频、视频等多种形式。
工具与资源上首页 5 次 · 2010–2022openculture.com淘气字符串:容易引发用户输入问题的字符串清单
Naughty Strings: A list of strings likely to cause issues as user-input data
一个 GitHub 仓库,收录了大量容易让程序出问题的用户输入字符串,如特殊字符、emoji、SQL 注入片段、系统命令等,供开发者用于测试输入处理逻辑。
工具与资源上首页 4 次 · 2015–2020github.com