TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Mastering Jq: Part 1

205 pointsby code-fasteralmost 5 years ago

20 comments

MattyRadalmost 5 years ago
By far the most useful jq operator I&#x27;ve used is recursive descent: `..`. It can take a positively heinous json blob and pick out the attributes&#x2F;keys you care about. For example:<p>Yikes:<p>&gt; <i>wget -O - -q &#x27;<a href="https:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;unixporn&#x2F;new.json&#x27;" rel="nofollow">https:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;unixporn&#x2F;new.json&#x27;</a> | jq</i><p>I just want the links:<p>&gt; <i>wget -O - -q &#x27;<a href="https:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;unixporn&#x2F;new.json&#x27;" rel="nofollow">https:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;unixporn&#x2F;new.json&#x27;</a> | jq &#x27;..|.permalink? | select(.)&#x27;</i>
评论 #23696508 未加载
评论 #23698200 未加载
anarcatalmost 5 years ago
I know I might be a dissenting opinion here, but I can never wrap my head around `jq`. I can manage `jq .`, `jq .foo` and `jq -r`, but beyond that, the DSL is just opaque to me.<p>And every time I try to learn, I get lost in a maze of a manpage:<p><a href="https:&#x2F;&#x2F;manpages.debian.org&#x2F;jq" rel="nofollow">https:&#x2F;&#x2F;manpages.debian.org&#x2F;jq</a><p>I mean there are useful tips in there, but I rarely find what I need. I usually find it simpler to write a Python script (which ships with `json` because it&#x27;s &quot;batteries included&quot;) and operate on lists and dicts the normal way...<p>It&#x27;s longer, but at least I don&#x27;t need to learn a new programming language (if we can call jq that...)
评论 #23699004 未加载
评论 #23698713 未加载
评论 #23699836 未加载
评论 #23699776 未加载
评论 #23697954 未加载
评论 #23700124 未加载
评论 #23700475 未加载
评论 #23698477 未加载
评论 #23700326 未加载
评论 #23704150 未加载
评论 #23698110 未加载
评论 #23698076 未加载
评论 #23697915 未加载
评论 #23707293 未加载
ryanneviusalmost 5 years ago
One of the most useful ways I&#x27;ve found to learn&#x2F;test queries in `jq` is by using it as a semi-REPL via fzf with `--preview`:<p><pre><code> echo &#x27;&#x27; | fzf --print-query --preview &#x27;cat example.json | jq {q}&#x27; </code></pre> Here&#x27;s a rough example of it in action: <a href="https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;y4WGyqcz1wWdiyxDofdPXKtdC" rel="nofollow">https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;y4WGyqcz1wWdiyxDofdPXKtdC</a>
评论 #23698891 未加载
llimllibalmost 5 years ago
This example is weirdly spelled:<p><pre><code> echo &#x27;{&quot;k1&quot;: [{&quot;k2&quot;: [9]}]}&#x27; | jq &#x27;.k1 | .[0] | .k2 | .[0]&#x27; </code></pre> is equivalent to<p><pre><code> echo &#x27;{&quot;k1&quot;: [{&quot;k2&quot;: [9]}]}&#x27; | jq &#x27;.k1[0].k2[0]&#x27; </code></pre> I kept waiting for the author to explain this, but they did not
评论 #23699300 未加载
评论 #23700930 未加载
JamesSwiftalmost 5 years ago
`jq` is up there with `nix` for me where I can sort of remember the DSL but always have to open the docs. I recently found <a href="https:&#x2F;&#x2F;jqplay.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jqplay.org&#x2F;</a> and its a huge help. I can crank scripts out much quicker now that I&#x27;m iterating with that.
评论 #23697132 未加载
jbotzalmost 5 years ago
jq is to cloud and &quot;Infrastructure as Code&quot; software what grep and awk are to unix software. It&#x27;s the glue that lets you combine the pieces into a system. It&#x27;s an odd language though, and not that easy to master. Tutorials like this one are helpful and needed... Thanks, Tyler!<p>However, there are quite a few typos in the examples... even the very first one is missing a quote and won&#x27;t parse if cut-and-pasted as is.
评论 #23696234 未加载
评论 #23696121 未加载
Rapzidalmost 5 years ago
I don&#x27;t know what it is, but anything more than moderately simple stuff gets super annoying for me with JQ.<p>After getting super frustrated with the documentation while trying to accomplish stuff that should have been straight forward, I created jsling so I could pipe output through node for JavaScript one-liners. Anything moderately complicated that doesn&#x27;t need to be portable; I just use that.<p>EDIT: I should mention that by &quot;moderately complicated&quot; I mean stuff that starts to get into the realm of joins, correlated sub queries, and the like.
ojhughesalmost 5 years ago
When I find myself reaching for jq, that’s a signal that maybe this bash script should be implemented in a language such as python or go
kureikainalmost 5 years ago
Another good one about JQ:<p><a href="https:&#x2F;&#x2F;programminghistorian.org&#x2F;en&#x2F;lessons&#x2F;json-and-jq#the-dot-" rel="nofollow">https:&#x2F;&#x2F;programminghistorian.org&#x2F;en&#x2F;lessons&#x2F;json-and-jq#the-...</a>
xonixalmost 5 years ago
I myself am developing a very similar tool [1] with somewhat saner (IMHO!) DSL. In fact this is a side-project from a JS lib I developed long ago [2].<p>My approach uses very simple ideas and is heavily based on JS internally. This is simple once you understand it [3] (thus saner DSL) and gives the whole power of JS in your hands.<p>I&#x27;ve also prepared a basic comparison jq vs jsqry based on some examples in article [4].<p>It&#x27;s worth noting that currently the CLI tool [1] is written in Java using Graal VM polyglot native image compilation. Thus HUGE executable size of 95 MB (sic!) because it bundles JS engine. I&#x27;m considering rewriting this to QuickJS by Fabrice Bellard [5]. This should make it MUCH smaller.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;jsqry&#x2F;jsqry-cli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jsqry&#x2F;jsqry-cli</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;jsqry&#x2F;jsqry" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jsqry&#x2F;jsqry</a><p>[3] <a href="https:&#x2F;&#x2F;jsqry.github.io&#x2F;#filtering" rel="nofollow">https:&#x2F;&#x2F;jsqry.github.io&#x2F;#filtering</a><p>[4] <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;xonixx&#x2F;d6066e83ec0773df248141440b18e8e4" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;xonixx&#x2F;d6066e83ec0773df248141440b18e...</a><p>[5] <a href="https:&#x2F;&#x2F;bellard.org&#x2F;quickjs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;bellard.org&#x2F;quickjs&#x2F;</a>
labelbiasalmost 5 years ago
Just recently used xq to convert and clean up a bunch of weird xml files to JSON. It was such a breeze.<p>I love how nested lists inside objects can expand into a particular one with the .[] operator.<p>For example:<p>{ a: [{b: 5}, {b: 3}], c: 5} can be transformed into: [{ b: 5, c: 5}, {b: 3, c: 5}] using jq &#x27;{c: .c, b: .a[].b}&#x27;<p>For heavily nested XMLs I can get a nice flat output.
评论 #23700313 未加载
moreatialmost 5 years ago
I wrote <a href="https:&#x2F;&#x2F;github.com&#x2F;moreati&#x2F;jq-filter" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;moreati&#x2F;jq-filter</a> for using jq with as a filter in Ansible.
Ericson2314almost 5 years ago
The author of jq is quite talented. I hope jq becomes a gateway drug.
coding123almost 5 years ago
Jq is quickly becoming one of the most useful tools in devops.
评论 #23697978 未加载
macca321almost 5 years ago
I reckon JMESPath should be first choice in 2020, as it&#x27;s properly specified, so you can find x-language implementations that behave the same.
maddybooalmost 5 years ago
jq is honestly one of my favorite tools ever. Super powerful yet simple.<p>I&#x27;ve written some relatively complex programs with it, e.g. this one [1] I just wrote to manipulate the output from neuron [2] to generate index files based on tags. It&#x27;s probably overkill&#x2F;not as efficient as it could be, but I wanted to be able to expand upon it later.<p>I think once you grok the basics, which can be a bit confusing at first, you can accomplish some amazing things with the help of a few of the more advanced features like reduce, to_entries&#x2F;from_entries, etc.<p>I really wish it was more actively developed, I feel like it actually has potential to be a semi-general-purpose functional programming language.<p>1: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;b50a047115d1dcf1f15c16a6f7b71e3c" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;b50a047115d1dcf1f15c16a6f7b71e3c</a><p>2: <a href="https:&#x2F;&#x2F;github.com&#x2F;srid&#x2F;neuron" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;srid&#x2F;neuron</a>
joluxalmost 5 years ago
Is there stuff jq can do that I can&#x27;t do with Invoke-RestMethod and manipulating objects in PowerShell?
评论 #23696450 未加载
评论 #23698158 未加载
评论 #23703200 未加载
ryanmccullaghalmost 5 years ago
My favorite way to use jq is as follows:<p>`tail -f my-log-file.json | jq`<p>This can be great if you&#x27;ve setup Nginx to log as JSON.
yiyusalmost 5 years ago
At first I thought this was about github.com&#x2F;timestored&#x2F;jq and was very confused.
pyuser583almost 5 years ago
jq really is an awesome tool.