By far the most useful jq operator I've used is recursive descent: `..`. It can take a positively heinous json blob and pick out the attributes/keys you care about. For example:<p>Yikes:<p>> <i>wget -O - -q '<a href="https://reddit.com/r/unixporn/new.json'" rel="nofollow">https://reddit.com/r/unixporn/new.json'</a> | jq</i><p>I just want the links:<p>> <i>wget -O - -q '<a href="https://reddit.com/r/unixporn/new.json'" rel="nofollow">https://reddit.com/r/unixporn/new.json'</a> | jq '..|.permalink? | select(.)'</i>
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://manpages.debian.org/jq" rel="nofollow">https://manpages.debian.org/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's "batteries included") and operate on lists and dicts the normal way...<p>It's longer, but at least I don't need to learn a new programming language (if we can call jq that...)
One of the most useful ways I've found to learn/test queries in `jq` is by using it as a semi-REPL via fzf with `--preview`:<p><pre><code> echo '' | fzf --print-query --preview 'cat example.json | jq {q}'
</code></pre>
Here's a rough example of it in action: <a href="https://asciinema.org/a/y4WGyqcz1wWdiyxDofdPXKtdC" rel="nofollow">https://asciinema.org/a/y4WGyqcz1wWdiyxDofdPXKtdC</a>
This example is weirdly spelled:<p><pre><code> echo '{"k1": [{"k2": [9]}]}' | jq '.k1 | .[0] | .k2 | .[0]'
</code></pre>
is equivalent to<p><pre><code> echo '{"k1": [{"k2": [9]}]}' | jq '.k1[0].k2[0]'
</code></pre>
I kept waiting for the author to explain this, but they did not
`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://jqplay.org/" rel="nofollow">https://jqplay.org/</a> and its a huge help. I can crank scripts out much quicker now that I'm iterating with that.
jq is to cloud and "Infrastructure as Code" software what grep and awk are to unix software. It's the glue that lets you combine the pieces into a system. It'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't parse if cut-and-pasted as is.
I don'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't need to be portable; I just use that.<p>EDIT: I should mention that by "moderately complicated" I mean stuff that starts to get into the realm of joins, correlated sub queries, and the like.
Another good one about JQ:<p><a href="https://programminghistorian.org/en/lessons/json-and-jq#the-dot-" rel="nofollow">https://programminghistorian.org/en/lessons/json-and-jq#the-...</a>
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've also prepared a basic comparison jq vs jsqry based on some examples in article [4].<p>It'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'm considering rewriting this to QuickJS by Fabrice Bellard [5]. This should make it MUCH smaller.<p>[1] <a href="https://github.com/jsqry/jsqry-cli" rel="nofollow">https://github.com/jsqry/jsqry-cli</a><p>[2] <a href="https://github.com/jsqry/jsqry" rel="nofollow">https://github.com/jsqry/jsqry</a><p>[3] <a href="https://jsqry.github.io/#filtering" rel="nofollow">https://jsqry.github.io/#filtering</a><p>[4] <a href="https://gist.github.com/xonixx/d6066e83ec0773df248141440b18e8e4" rel="nofollow">https://gist.github.com/xonixx/d6066e83ec0773df248141440b18e...</a><p>[5] <a href="https://bellard.org/quickjs/" rel="nofollow">https://bellard.org/quickjs/</a>
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 '{c: .c, b: .a[].b}'<p>For heavily nested XMLs I can get a nice flat output.
I wrote <a href="https://github.com/moreati/jq-filter" rel="nofollow">https://github.com/moreati/jq-filter</a> for using jq with as a filter in Ansible.
jq is honestly one of my favorite tools ever. Super powerful yet simple.<p>I'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's probably overkill/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/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://gist.github.com/b50a047115d1dcf1f15c16a6f7b71e3c" rel="nofollow">https://gist.github.com/b50a047115d1dcf1f15c16a6f7b71e3c</a><p>2: <a href="https://github.com/srid/neuron" rel="nofollow">https://github.com/srid/neuron</a>