Extracting Objects Recursively with Jq
til.simonwillison.net
til.simonwillison.net
It is essentially similar to jqplay but local.
I didn't use jid/jiq because jid uses go-simplejson, which is nowhere near as powerful as jq, and jiq seemed very buggy when I used it and it felt like it was hacked together. Plus there was no where to change jq's arguments while running it.
I'm sure this function can be improved on, but this has been good enough for me so far.
Also, I run gojq[2] instead of jq. It is a drop-in replacement for jq but is written in Go, and has some improvements over jq such as bug fixes, support for yaml input, and it also provides more helpful error messages.
[0] https://github.com/hoshsadiq/dot_files/blob/master/zshrc.d/m...
Dependencies: tmux, nodemon, less, jq, vim
[1]: https://gist.github.com/psacawa/e63c4e25a8b0405309d3a03b6b50...
https://github.com/kbd/setup/blob/master/HOME/bin/fzr
It's just an fzf wrapper that sets up temporary files and so on. It works really well; it's amazing all the things one can use fzf for.
It's the classical tool, like sed, like awk, like a ton of unix utility that at first they seem to you easy to use, then you have to do something complex and you start abusing them, by piping things multiple times into jq, and you end up writing things like this:
echo $json | jq "something $(echo $variable | jq 'something else' | tr '"' '\'') | sed 's/"/\\'/g" | jq "another js invocation" | awk '...' > file2.json
I stopped using jq after realizing that I was wasting my time by trying to fix a script that used jq and didn't managed quoting correctly, trying to use different kind of quotes, even filtering the input before passing it to jq with tr replacing things. It's just another tool prone to abuse like sed, awk, tr, cut or similar things.I thought why I'm wasting my time on a tool that has a complex and limited DSL when I can write a clean python script in 10 minutes to do the same things that is easier to write, to read and most importantly to maintain.
To me a script that has to manipulate JSON should be written in an high level programming language like Python, and not be abused with tool as jq and stuff. Even I there is an already existent big bash script that you don't want to rewrite and you have to do some json processing in it... you can write an inline python script like this:
python3 <<PYEND
## your python code
PYEND
Also jq is another dependency to a script that must be installed.If you actually "get into" jq you find out that it's a significantly neater language than it appears on the surface. Firstly it does allow you write multi-line scripts, and things start to look a lot neater once you do. Secondly it's actually a real, working, functional programming language, which allows very succinct expression of ideas which, in python, would likely require the reader to track state across explicit loops and the like.
Once you dig into the manual, you also tend to discover that a lot of the things that cause you to string multiple jq invocations together aren't actually necessary because there are quite sensible ways of handling them in-language.
It's quite laughable though to tout python over jq because of it adding a dependency. Perhaps if you're already embedded in python-land and all your environments already have python - but many (most?) of us are increasingly targeting extremely minimal container image environments. In that case, adding python is a much larger and more complex dependency than jq's single 3.8MB binary.
As someone who has to read an awful lot of other peoples deployment scripts, it's also quite nice when I see jq because it loudly advertises "all I'm doing here is mangling one piece of json into another! no side effects!". I'd much rather follow the thread of execution into that than some mystery ruby script any day.
This is the problem. Is yet another language that someone has to learn and know, like the ton of other UNIX commands that have their own DSL.
> Once you dig into the manual, you also tend to discover that a lot of the things that cause you to string multiple jq invocations together aren't actually necessary because there are quite sensible ways of handling them in-language.
Nice. I can dig into the manual and spend a day to learn it, but I don't have that time. I have a script to fix, I know how to program in python, and throw away the jq code and substitute it with 10 lines of python in a minute, and problems solved.
> It's quite laughable though to tout python over jq because of it adding a dependency
If jq enters your codebase then everywhere you have to install it. It's not that difficult (but not trivial on Windows), but it is annoying, you run a script and you then find out that you don't have jq installed and you need to install it.
Also what is faster: eliminating the need for the jq command in a script, or installing jq on tens of different systems with different operating systems?
No, where I work I established a rule that every script should be written in python and should use only the standard library, with a few exception (e.g. we work with AWS so boto3 is an exception). If not every developer did use whatever tool they thought it was cool (like jq), write a ton of bash spaghetti code with all these tools used together, and forced every other developer to install them on their system (and cause a lot of work to the IT, i.e. me to do so if they weren't able and fix all the problem).
> Perhaps if you're already embedded in python-land and all your environments already have python
Python is everywhere. Every Linux distribution have a python interpreter in them, same for macOS, and in Windows nowadays you install directly from the store with one click. The problem is that if jq enters the codebase then of course every developer machine has to have it installed. This is annoying.
function jqsk {
sk --tac --ansi --regex --query . --multi --interactive --cmd '{}' \
--bind 'enter:select-all+accept,ctrl-y:select-all+execute-silent(for line in {+}; do echo $line; done | pbcopy)+deselect-all' \
--cmd-history=${HOME}/.sk_history --cmd-history-size=100000 \
--no-clear-if-empty \
--cmd-query "cat $1 | jq --raw-output --color-output --exit-status '.'"
}
I run it like `jqsk file.json` and then change the `'.'` in the cmd-query to whatever I'm trying to do with jq.I'll definitely look into some of the other mentioned solutions for this though. I settled on skim a year+ ago and haven't revisited.
I considered forking jid/jiq and using gojq as a library, but I ended up not going down that route because of reasons that I cannot remember. I also considered using a tui or something but FZF has so much already implemented and has a lot of it right, and I didn't particularly feel like re-inventing the wheel.
I think JQ could really benefit from having a classic programming language style "book", like "The AWK Programming Language". JQ is fundamentally a functional programming language with semantics that are not obvious reading its current docs.
I don't mean to denigrate the current docs, writing documentation is hard!
And the docs for yq (the jq for YAML) are 100 times worse. The documentation feels like it was written by aliens. Even incredibly simple things are impossible for me to find in the yq docs.
Unfortunately (I guess this is a big reason why people don't use it), it doesn't sort the comments – if you need the orders, you'll have to parse HN HTML (or just use the official API).
Still just two requests (the HN site, the Algolia API) is much better than recursively requesting a hundred requests, so I use this approach in my client[2].
[0]: https://hn.algolia.com/api
Using for example Scryer Prolog, we can conveniently relate the data to a flat list of items with Prolog's built-in grammar mechanism, definite clause grammars (DCGs):
flat_json(JSON) -->
{ JSON = {A,B,C,D,E,F,_:Cs} },
[{A,B,C,D,E,F}],
flat_items(Cs).
flat_items([]) --> [].
flat_items([I|Is]) -->
{ I = {(A,B,C,D,E,_:Cs)} },
[{A,B,C,D,E}],
flat_items(Cs),
flat_items(Is).
Sample query, using the example JSON data from the article: ?- JSON = {
"id": 27941108,
"created_at": "2021-07-24T14:15:05.000Z",
"type": "story",
"author": "edward",
"title": "Fun with Unix domain sockets",
"url": "https://simonwillison.net/2021/Jul/13/unix-domain-sockets/",
"children": [
{
"id": 27942287,
"created_at": "2021-07-24T16:31:18.000Z",
"type": "comment",
"author": "DesiLurker",
"text": "<p>one lesser known...",
"children": []
},
{
"id": 27944615,
"created_at": "2021-07-24T21:26:33.000Z",
"type": "comment",
"author": "galaxyLogic",
"text": "<p>I read this from Wikipedia...",
"children": [
{
"id": 27944746,
"created_at": "2021-07-24T21:49:07.000Z",
"type": "comment",
"author": "hughrr",
"text": "<p>Yes although I ...",
"children": []
}
]
}
]
},
phrase(flat_json(JSON), Cs),
maplist(portray_clause, Cs).
yielding the flat list of entries, as desired: [{("id":27941108,"created_at":"2021-07-24T14:15:05.000Z","type":"story","author":"edward",...)},
{("id":27942287,"created_at":"2021-07-24T16:31:18.000Z","type":"comment",...)},
{("id":27944615,"created_at":"2021-07-24T21:26:33.000Z","type":"comment",...)},
{("id":27944746,"created_at":"2021-07-24T21:49:07.000Z","type":"comment",...)}]Prolog was the one language I couldn’t get my head around in the programming languages class I took at school.
* it is very potent
* it has extensive documentation, written more like parabolas and new-age sorcery than actually helpful content
* it improves your search skills greatly, for many internet results try to give sense to its format
* it gives great satisfaction after you've died-and-retried 300x a whole afternoon for a command you'll only need once.
* it looks cool to use jq instead of [any language you're already familiar with].
Yes, I'm being sarcastic, yet honest. Note that I still use it and recommend it for simpler use cases though.
_[shrug]_
well, I would but the result is "too long for a HN comment" so here a bunch is sniped out of the middle (unedited the result would currently be 140 lines)
curl -s https://hn.algolia.com/api/v1/items/27941108 | ~/bin/json2jqpath.jq
.
.author
.children
.children|.[]
.children|.[]|.author
.children|.[]|.children
.children|.[]|.children|.[]
.children|.[]|.children|.[]|.author
.children|.[]|.children|.[]|.children
.children|.[]|.children|.[]|.children|.[]
.children|.[]|.children|.[]|.children|.[]|.author
.children|.[]|.children|.[]|.children|.[]|.children
.children|.[]|.children|.[]|.children|.[]|.children|.[]
<snip>
...
</snip>
.children|.[]|.children|.[]|.created_at
.children|.[]|.children|.[]|.created_at_i
.children|.[]|.children|.[]|.id
.children|.[]|.children|.[]|.options
.children|.[]|.children|.[]|.parent_id
.children|.[]|.children|.[]|.points
.children|.[]|.children|.[]|.story_id
.children|.[]|.children|.[]|.text
.children|.[]|.children|.[]|.title
.children|.[]|.children|.[]|.type
.children|.[]|.children|.[]|.url
.children|.[]|.created_at
.children|.[]|.created_at_i
.children|.[]|.id
.children|.[]|.options
.children|.[]|.parent_id
.children|.[]|.points
.children|.[]|.story_id
.children|.[]|.text
.children|.[]|.title
.children|.[]|.type
.children|.[]|.url
.created_at
.created_at_i
.id
.options
.parent_id
.points
.story_id
.text
.title
.type
.url
[0] https://github.com/TomConlin/json_to_pathsI like the explicit separators when I'm reading them instead of writing them. Helps the individual steps stand out for me.
Is there a reason other than a more compact line?
What I mean is this: the functionality offered by jq (parsing json on the command line and extracting what you need from it) is really needed in many modern data processing tasks, but jq's DSL is one of the most horrible thing I've had to learn in recent years.
The only way a casual user of that thing can hope to succeed in actually crafting a working jq query is pray that there is a stack overflow topic answering his exact need.
Here's the way I use jq 99% of the time:
cat somefile.json | jq . | <long pipeline of traditional, well though-out unix text processing tools such as sed, grep, awk, cut, etc...>
And when this doesn't cut it, I write a python script.Say you need to jump into a running container that isn't running as root, doesn't have sudo/su, and doesn't even have packaging tools. The only practical way to quickly install a debugging tool is either 1) volume-mount a file, 2) copy in a file, or 3) curl/wget. So a small(ish) static binary is the best thing you can hope for. I've actually built a dozen static versions of common debugging tools just to make this easier.
If I had more free time I would port jq's semantics to BusyBox and gain that much more utility out of that lovely little environment.
https://github.com/DaveJarvis/github-email/blob/master/githu...
def my_func:
del(.children),
(.children[] | my_func)
;
[my_func]
(The function first emits its input object without the "children" field, then emits each element of the "children" array separately, after passing each through a recursive call of the function.The ; char ends the function definition.
Finally, the function is called once on the top-level input and all emitted results are collected into an array.)
This is more verbose than recurse() but may sometimes be easier to understand if you want to know what is going on.
It does exist as a library for Python, but unfortunately it links to jq’s C library. That means if I use it, then whoever uses my program has to compile C code whenever they pip install my program. I’m unwilling to do that, because history shows that as t approaches infinity, the chance of C code failing to compile during a pip install approaches 100%.
If anyone’s looking for a challenge, please implement all of jq’s logic in pure Python. It would be so wonderful…
Fortunately there was a Visual Studio extension that could export objects into JSON/XML/C# representation; then all I had to do was to diff the object dump. Was often called a "Jedi Wizard" for using this trick lol
What I think I want is a syntax closer to css selectors. What I think I'm going to have to do is really stop and learn jq. It looks like some of the links in here may help.
I often build my Jq steps in the playground before committing.
With the same amount of benefits and drawbacks.
You an author or something?