That’s what I thought too, when I read the title. To clarify: This tool formats jq commands, not JSON itself.
which is a long way of saying: or else what? There's 100% no way that I'm going to ever, ever use <<python3 -c "import json, sys; print(json.load(sys.stdin)[...ohgawd...]">> and if you are, then more power to ya and jq apparently doesn't solve a problem you have
PS. Honestly, it's pretty close.-
jqfmt -ob -ar -op pipe
It would be better if the tool enabled a common set of rules by default, so that `echo ... | jqfmt` actually did something useful :)https://github.com/noperator/sol
I actually wrote jqfmt because I needed it for sol :)
PS. Happily, featured here:
Anyway whether or not this tool is advisable its definitely cool, nice work!
Entirely correct, this point.-
PS. May I also appreciate your comment, as far as form? You made both, valid, points.-
Give you a nice javascript interface to do similar types of processing to what I would do with jq.
rwiggins•4h ago
I'll use this opportunity to plug the one-liner I use all the time, which summarizes the "structure" of a doc in a jq-able way: https://github.com/stedolan/jq/issues/243#issuecomment-48470... (I didn't write it, I'm just a happy user)
For example:
(except I have it aliased to "jq-structure" locally of course. also, if there's a new fancy way to do this, I'm all ears; I've been using this alias for like... almost a decade now :/)In the spirit of trying out jqfmt, let's see how it formats that one-liner...
Not bad! Shame that jqfmt doesn't output a newline at the end, though. The errant `%` is zsh's partial line marker. Also, `-ob -ar -op pipe` seems like a pretty good set of defaults to me - I would prefer that over it (seemingly?) not doing anything with no flags. (At least for this sample snippet.)jzelinskie•4h ago
I'm a big fan of jq, having written my own jq wrapper that supports multiple formats (github.com/jzelinskie/faq), but these days I find myself more quickly reaching for Python when I get any amount of complexity. Being able to use uv scripts in Python has considerably lowered the bar for me to use it for scripting.
Where are you drawing the line?
Bluestein•4h ago
rwiggins•3h ago
But as an example of about where I'd stop using jq/shell scripting and switch to an actual program... we have a service that has task queues. The number of queues for an endpoint is variable, but enumerable via `GET /queues` (I'm simplifying here of course), which returns e.g. `[0, 1, 2]`. There was a bug where certain tasks would get stuck in a non-terminal state, blocking one of those queues. So, I wanted a simple little snippet to find, for each queue, (1) which task is currently executing and (2) how many tasks are enqueued. It ended up vaguely looking like:
which ends up producing output like (assuming queue 0 was blocked) I think this is roughly where I'd start to consider "hmm, maybe a proper script would do this better". I bet the equivalent Python is much easier to read and probably not much longer.Although, I think this example demonstrates how I typically use jq, which is like a little multitool. I don't usually write really complicated jq.
dotancohen•1h ago
petercooper•4h ago
rwiggins•3h ago
naniwaduni•40m ago
Speaking of overhead, though, it turns out that the implementation of walk/1 (https://github.com/jqlang/jq/blob/master/src/builtin.jq#L212) will actually run the filter on every element of an array, even though we're about to throw most of them out, which we can eliminate by writing the recursion explicitly:
which gets the operation down from ~200 ms on my machine (not long enough to really get distracted, but enough to feel the wait) to a visually instant ~40 ms (which is mostly just the cost of reading the input). Now we can golf it down a little more: (the precedence here actually allows us to eliminate the parens here...) And, inaccessibility of the syntax aside, I think this does an incredible job of expressing the essence of what we're trying to do: we trim any array down to its first element, and then recursively apply the same transformation throughout the structure. jq is a very expressive language, it just looks like line noise...Bluestein•33m ago
PS. Also, if I may l, thanks for the walkthrough - I'd be clapping with just the short form at the end, but the reasoning is appreciated.-
jdc0589•3h ago
crashabr•1h ago
[0] https://github.com/adamritter/fastgron
naniwaduni•1h ago