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
Users (i.e. not implementors) usually also don't read the standard – they read the docs (ideally containing lots of examples on top of a dry enumeration of options), or today indeed ask an LLM.
I get a lot of use out of `jq` even though I prefer sounder systems than JSON.
It basically dumps the flattened js equation form of the json. You can then sed grep that whatever. Then you can gron --ungron and it will make it JSON again. It works beautifully, check out the examples in their docs.
fx.wtf is also very nice as an interactive viewer with vim movements.
Then jo for creating json.
I found it much easier to use these three + core utils as compared to learning jq, lol.
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.
Recently, I found myself wanting to do a join by filename on two sets of about 300,000 files. Tried bashing my head against jq with INDEX and various tricks and couldn't get the runtime below minutes.
Then I just gave up, fired up Python, loaded the dataset into Pandas, and did a join. Completed too fast to notice.
Did the `join` command help?
jq is sed for json data. gofmt is a GO source formatter jqfmt is like gofmt a go source formatter, but for json. So jqfmt is really json beautifier...
Anyone with an ASR-33 for sale? rq?
rwiggins•9h 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•9h 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•9h ago
rwiggins•7h 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•5h ago
easton•4h ago
https://docs.astral.sh/uv/guides/scripts/
Makes it a snap to have a one file python script without having to explicitly pip install requests or whatever into a venv.
wonger_•4h ago
dotancohen•2h ago
I could imagine a Python wrapper script that parses the Python for import statements, then prepends these comments and passes it off to uv. Basically automating the process.
petercooper•8h ago
rwiggins•8h ago
naniwaduni•5h 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 perceptually 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•5h 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•8h ago
crashabr•6h ago
[0] https://github.com/adamritter/fastgron
naniwaduni•6h ago