by anarcat · 6y ago · 0 comments
And every time I try to learn, I get lost in a maze of a manpage:
https://manpages.debian.org/jq
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...
It's longer, but at least I don't need to learn a new programming language (if we can call jq that...)
May I suggest "gron"? As compared to jq it is simpler (good!) has less features (good!) has no DSL (good!) and it is less powerful (good!, in many cases). It is a tool that "expands" json into standalone lines, and you can further process them using the standard tools grep, sed, cut, sort, awk...
EDIT: for example, if you have this json:
{
"outdir" : "out3",
"data" : [
{"img" : "img_01.jpg"},
{"img" : "img_02.jpg"}
],
"roi" : {
"x" : 150,
"y" : 150,
"w" : 700,
"h" : 700
},
"margin_h": 20,
"margin_v": 5,
"tile_size" : 300,
"resolution": 0.5
}
running it through gron produces this outdir = "out3"
data[0].img = "img_01.jpg"
data[1].img = "img_02.jpg"
roi.x = 150
roi.y = 150
roi.w = 700
roi.h = 700
margin_h = 20
margin_v = 5
tile_size = 300
resolution = 0.5
that some people find really convenient to deal with.I don't think this is uncommon. People generally reach for jq for simple path expression evaluation against JSON objects, and never get deeper into it than that. It seems like the kind of thing you could re-implement yourself in an afternoon. However, as soon as you start taking advantage of some of the more complex functionality--say, a program like `.entries[] | select(.size > 1024) | .name`--there's a disquieting feeling of "what the hell is actually happening here?"
I took some time a while back to really dig into how jq programs work, and was surprised to discover how deep and powerful jq really is, while being built out of some very simple fundamental building blocks. Most of the jq builtin functions are actually implemented in jq itself[1]; even more of them could be, but aren't for the sake of performance. And the power and self-consistency of jq made sense when I found out that the creator Stephen Dolan is an "actual" Computer Scientist (in the academic sense) with extensive programming language research experience[2].
The main obstacle I see for most people is the lack of of an accessible introduction to how jq actually works when it comes to streams, function calls, generators, and backtracking. They're explained somewhat in the docs and on the project wiki[3][4][5], but in a fairly blunt way that assumes existing familiarity with terminology and concepts. It takes some effort to learn initially, but once you understand it, everything falls into place. I'm hopeful that subsequent installments in this "mastering" series can explain how the jq model works in an approachable way.
[1] https://github.com/stedolan/jq/blob/master/src/builtin.jq
[2] http://stedolan.net/research/
[3] https://stedolan.github.io/jq/manual/#Advancedfeatures
[4] https://github.com/stedolan/jq/wiki/Advanced-Topics
[5] https://github.com/stedolan/jq/wiki/Internals:-backtracking
Yq [1] syntax for that is much better, and yq can accept JSON files too.
This is not to shame the developers but to voice our issues with it since this seems to be the prevailing sentiment, perhaps some brave soul will try and simplify it for human brains.
The reason it's popular partly because it's available from both Centos/Debian based. Just use `yum` and `apt`. Most of time, you maybe limited with what have on a container. And `jq` may even pre-install by DevOps.
Second is it play well with unix tool, pipe and friend.
Any more alternates to Python or Node that are lightweight to add to a project? I'd rather use Node but that's a massive dependency for a non-Node project.
In python, you can have a jq like experience with
def f(data):
# put code here
data = json.load(sys.stdin)
output = f(data)
print(json.dumps(output))I think the examples for any() are any(true, false) and any(true, true). Fair play but not super helpful if I’m new and trying to understand how this fits into a typical jq filter.
I've made jql[0] for that reason.
Check it out, it's less featureful, but much simpler with a uniform lispy syntax.
I would like to see a document that talks about jq from this more abstract perspective and what capabilities exist in this context. For example, can I select two arbitrary branches? Can I merge / move branches? Once we have a grasp of tree manipulation capabilities that are made available through the DSL we can lookup the syntax. Can I select a node and apply filters to it's children? There are so many ways in which a tree can be manipulated and I personally need a higher level description of what I can accomplish with jq.
jq has looping, branching, recursion (with tail recursion optimization), variable assignment, modules, I/O... Yeah, it's a programming language.
I think the only way would be a long web page with a massive list of examples to "solve problem X".
But the DSL is ...byzantine, to say the least.
Spending one hour or so actually learning jq is a good time investment.