TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Jq – A Command-line JSON processor

451 点作者 geekrax大约 10 年前

24 条评论

useerup大约 10 年前
jq is awesome, a real achievement.<p>At the same time it demonstrates everything that is wrong with traditional shells and what PowerShell gets so right.<p>jq is <i>not</i> a &quot;Unixy&quot; tool in the sense that it should do one thing and do it right. jq implements its own expression language, command processor complete with internal &quot;pipelines&quot;. Why would a tool need to do that? find is another utility that does many, many other things than to &quot;find&quot; items: It executes commands, deletes objects etc.<p>Consider this challenge that included parsing json, filtering, projecting and csv-formatting output: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9438109" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9438109</a><p>Several solutions uses jq - to good effect. But the PowerShell solution uses <i>PowerShell</i> expressions to filter, sort and project items.<p>The problem is - at the core - that the traditional command tools are severely restricted by only being able to rely on a text pipeline convention. You cannot parse json and send the &quot;objects&quot; along to another tool. Well, you can, if the json tree is extremely basic - like 2 levels.<p>PowerShell also has a json parser tool: <a href="https:&#x2F;&#x2F;technet.microsoft.com&#x2F;en-us&#x2F;library&#x2F;hh849898.aspx" rel="nofollow">https:&#x2F;&#x2F;technet.microsoft.com&#x2F;en-us&#x2F;library&#x2F;hh849898.aspx</a>. It is called ConvertFrom-Json (follows the verb-noun convention) and is distributed as part of PowerShell (built-in if you want - but that&#x27;s a misnomer for PowerShell modules).<p>It is extremely simple - it doesn&#x27;t even take any parameters - it just converts a string (parses it as json) and outputs the object&#x2F;objects which can be arbitrarily complex. It truly does only one thing. If you want to select specific properties, sort, group etc you do it with other PS tools like select (alias for Select-Object), sort (alias for Sort-Object), group (alias for Group-Object) etc. If you want to convert back to json you use ConvertTo-Json.
评论 #9447516 未加载
评论 #9447425 未加载
评论 #9448522 未加载
评论 #9448017 未加载
评论 #9448336 未加载
评论 #9447461 未加载
评论 #9449115 未加载
评论 #9448197 未加载
评论 #9447623 未加载
xahrepap大约 10 年前
I&#x27;ve been using this for a couple months now. With httpie[1], it&#x27;s become as essential to my work day as Grep.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;jakubroztocil&#x2F;httpie" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jakubroztocil&#x2F;httpie</a>
评论 #9447278 未加载
评论 #9447478 未加载
danso大约 10 年前
Love, <i>love</i> jq...after awhile, I realized I was twisting my brain too much to get bash to do what it wasn&#x27;t meant to do, but I got a lot of mileage of just playing with APIs because of how jq made it so easy. I came up with some fun lessons on how to use jq and other CLI-tools to tour the Spotify API [1] and to mash up images from Instagram [2]<p>[1] <a href="http:&#x2F;&#x2F;www.compciv.org&#x2F;recipes&#x2F;data&#x2F;touring-the-spotify-api&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.compciv.org&#x2F;recipes&#x2F;data&#x2F;touring-the-spotify-api&#x2F;</a><p>[2] <a href="http:&#x2F;&#x2F;www.compciv.org&#x2F;recipes&#x2F;data&#x2F;api-exploration-with-gmaps-instagram&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.compciv.org&#x2F;recipes&#x2F;data&#x2F;api-exploration-with-gma...</a><p>(note: while I say that I like the command-line, I&#x27;m not die-hard enough to have stopped to learn awk, so take that fwiw)
piva00大约 10 年前
Using it for parsing AWS CLI responses, it&#x27;s been a sweet tool to parse those JSON responses from AWS, most of the time I&#x27;d fire some python script to parse the data and now with jq I can keep most of the one-time scripts limited to bash only, if I need something more maintainable or longer living then I&#x27;ll drop a script in python or ruby.
评论 #9450385 未加载
评论 #9450116 未加载
themartorana大约 10 年前
Lots of previous discussions about this on HN (for instance [0]) but still a sweet tool.<p>[0] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5734683" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5734683</a>
devy大约 10 年前
Jq is truly amazing swiss army knife for JSON viewing&#x2F;processing in command line.<p>Btw, I&#x27;ve seen people posting this github link at least 10 times in the past. I thought HN filters dupes, what gives?<p><a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=http:%2F%2Fstedolan.github.io%2Fjq%2F&amp;sort=byPopularity&amp;prefix&amp;page=0&amp;dateRange=all&amp;type=story" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=http:%2F%2Fstedolan.github.io%...</a>
评论 #9447589 未加载
yunong大约 10 年前
For a similar tool written in javascript, check out json (<a href="https:&#x2F;&#x2F;github.com&#x2F;trentm&#x2F;json" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;trentm&#x2F;json</a>). I use both.
andrewd18大约 10 年前
Love JQ, been using it for the past year. I&#x27;m very much looking forward to the regex support in 1.5.
_RPM大约 10 年前
I love that it is written in C and not Go, or Rust, Java
rmgraham大约 10 年前
I know it is right on the linked page, but it is useful enough I think it deserves a direct link in any conversation:<p><a href="https:&#x2F;&#x2F;jqplay.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jqplay.org&#x2F;</a>
blacksqr大约 10 年前
Compare: rl_json -- Extends Tcl with a json value type and a command to manipulate json values directly. The commands accept a path specification that names some subset of the supplied json entity. The paths used by [json] allow indexing into JSON arrays by the integer key.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;RubyLane&#x2F;rl_json" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;RubyLane&#x2F;rl_json</a>
aggregator-ios大约 10 年前
For those on OS X, check out JSON Query: <a href="https:&#x2F;&#x2F;itunes.apple.com&#x2F;us&#x2F;app&#x2F;json-query&#x2F;id953006734?mt=12" rel="nofollow">https:&#x2F;&#x2F;itunes.apple.com&#x2F;us&#x2F;app&#x2F;json-query&#x2F;id953006734?mt=12</a><p>It&#x27;s not nearly as powerful as jq, but for simple querying and downloading JSON (with POST body and HTTP headers), I find it quite useful.<p>Disclaimer: I built it.
评论 #9448865 未加载
platz大约 10 年前
I recently had to consume an xml service; it would&#x27;ve been great to have something like Jq for xml. For some reason I wasn&#x27;t really interested in xml -&gt; json conversion as a first step. I also found a bunch of xml cli tools, but they are older, had to learn things like XQuery (which actually few tools today seem to support).
评论 #9450771 未加载
评论 #9448842 未加载
评论 #9448457 未加载
doki_pen大约 10 年前
Nice. I made jipe[0] a while back to handle common troubleshooting tasks with streams of json objects. I still use it quite a bit. I didn&#x27;t see anything about streaming in the jq docs.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;dokipen&#x2F;jipe" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dokipen&#x2F;jipe</a>
评论 #9449023 未加载
feld大约 10 年前
This appears to be in FreeBSD ports already, so just<p><pre><code> pkg install jq </code></pre> and you can have it :)
kalehrishi大约 10 年前
We are using jq to parse JSON output from various API in shell scripts. It served us well even for large JSON strings. If you want to write small scripts and parse some JSON in it then jq is best fit.
AdrianRossouw大约 10 年前
i love it so much.<p>i&#x27;ve used it for things like migrating between json schemas, and even converting normal json into elasticsearch bulk imports on the fly.
XorNot大约 10 年前
How weird - I just started using this for some simple scripting against a rest interface. Its a great tool.
klinquist大约 10 年前
I love JQ. I use it along with Ansible&#x27;s AWS dynamic inventory script to manage my instances.
jimmy0x52大约 10 年前
No mention of jsawk in here? We use it quite a bit for shell scripts, works great.
beamatronic大约 10 年前
I appreciate jq and use it almost every day. At the same time, I recently had a task that was easier to do in Node.js. I had a file containing a JSON object with a structure like so: [ { }, { }, ... ] and I wanted to turn each object into a separate file.
评论 #9450479 未加载
drdoom大约 10 年前
Looks interesting. Given this json, [{&quot;name&quot;:&quot;john&quot;, &quot;grades&quot;:[1,2,3]}, {&quot;name&quot;:&quot;jane&quot;, &quot;grades&quot;:[4,5,6]}], can it print it out as this: john,1 john,2 john,3 jane,4 jane,5 jane,6
评论 #9448710 未加载
misiti3780大约 10 年前
one of the most useful command lines tools in my toolbox these days - thank you for all that have contributed to this masterpiece.
shmerl大约 10 年前
I&#x27;m using it in bash scripts - it&#x27;s a very good tool.