Illustrated Jq Tutorial
mosermichael.github.io
mosermichael.github.io
"Cmdlets", basically commands used in Powershell, output "objects" instead of the streams of text used in a more classical shell. Powershell has built-in tools to work with these objects. For example, you can take the output from one Cmdlet, pipe it through `SELECT` with a list of fields specified, and get a stream of objects only containing those fields. Other operations can be performed against those objects as well, such as filtering and whatnot.
Back to normal nix commands, we're starting to see more and more commands introduce direct JSON support [1]. There are even tools to translate output from common commands into JSON [2]. We'll probably see `jq` shipped directly with modern distros soon. Eventually we'll reach a tipping point where it's expected that command supports JSON output. Tools like `awk`/`sed` might get updated to have a richer support for JSON. Finally, we'll have ubiquitous Powershell-like capabilities on every nix machine.
Powershell _is_ available on Linux. The model of piping objects instead of JSON is both powerful and more efficient (For example, there's no redundant keys like in a stream of JSON objects, leading to less moving bytes, like how CSV headers aren't repeated with every row. Plus, binary data is smaller than text.) But, most developers are hesitant to switch out their shell and existing workflows for a completely new tool, which is why Powershell will likely only be adopted by a small subset of sysadmins.
[1] https://daniel.haxx.se/blog/2020/03/17/curl-write-out-json/
IMO, powershell should be added to ALL mainstream distros as first order citizen. There is no downside to that given that MS is now legit FOSS player and that anybody can fork in case something goes wrong along the way...
So Microsoft could very well sue anyone using Powershell, using patents. We don't know if they would win, as there might (or might not) be some protection in a "implicit" grant that a copyright license give. But it would be a fight most organizations would not be willing to take, and would settle out of - a win for Microsoft in practical terms. It is not long ago since they did this with FAT32 against practically all Android manufacturers.
Apache 2.0 would be the permissive license to go for if desiring to guarantee safety from patent issues. http://en.swpat.org/wiki/Patent_clauses_in_software_licences
> deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so
If somebody pulled this trick on me, and I were a judge, I would punish both laywer and a company he is representing. Its simply emberrasing and belitteling toward human intelect.
Also:
> the Open Source Initiative has stated its view that all open source licenses implicitly include a patent grant
Thx for the info.
And back to nix commands, libxo is used by a chunk of the FreeBSD base tools to offer output in JSON, amongst other things: https://github.com/Juniper/libxo
-% ps --libxo=json,pretty
{
"process-information": {
"process": [
{
"pid": "52455",
"terminal-name": "5 ",
"state": "Is",
"cpu-time": "0:00.00",
"command": "-sh (sh)"
},
-% uptime --libxo=json,pretty
{
"uptime-information": {
"time-of-day": " 8:34p.m.",
"uptime": 1730360,
"days": 20,
Be nice to see more tools converted.Every time I open it up I just nope back out, and most of the docs I peeked at didn't draw me in.
What I would like to see is some sort of stddata stream be offered by the kernel itself so devs won't have to switch their shells and object manipulation can be a standard.
Here's a demo of a prototype live programming environment I made for jq, which similarly shows step-by-step views of the data but also gives live feedback as you construct your pipeline:
https://twitter.com/geoffreylitt/status/1161033775872118789
By the end of that Twitter thread I ultimately morphed it into a tool for building interactive GUIs (eg, get API data in JSON, use jq to morph it into the right shape for your UI, output to a HTML template).
I'm sure it's fine reading material, but if we allowed Show HN to be reading material, every submission would be a Show HN.
[1]: In 2001, prior to the huge popularity of JSON, it is an ASCII-coded dict+string+int+list format used in Bittorrent .torrent files.
edit: the article does not have <p> tag. so reader view is not triggered.
I liked jq but liked json, a similar npm package, a little bit better for simple tasks.
You can find more about it here: https://github.com/trentm/json
As a JS dev I tend to have node installed anyhow so I just use a shell alias to wrap ‘node -pe’ these days. It’s not really for shell scripts but it’s great for quick every day usage. Plus you can use JS if needed instead of their DSL.
Here the code for the alias in my shell profile: https://github.com/KylePDavis/dotfiles/blob/master/.profile#...
https://github.com/tomnomnom/gron
Some options of gron I use often:
--stream, which treats the input as "JSON lines" format
--ungron, which converts from the flat format back to JSON
For example, YAML supports the concept of reusable fragments. You can define a fragment in one place and reference it further down in your YAML file. A JSON converter could take the final YAML output and turn it into JSON but you would lose the context of the fact that in the original YAML it was an included fragment and not just the same section repeated a few times.
Yeah, no straightforward translation of references to JSON. You could provide both translation ramps, but it would be an implementation-specific convention and not something other JSON tools understood.
yq r -j test.yaml > test.json
cat file.yaml | jc --yaml
It can do everything with json as well and convert between
> I've tried `yq` briefly but weirdly enough it doesn't seem to accept standard input in the way that jq does (ie pipe in some json, and output some pretty json)
One suggestion is to use with_entries as a replacement for the 'to_entries | map(...) | from_entries' pattern. For example:
jq '.metadata.annotations | with_entries(select(.key == "label1"))'
is equivalent to jq '.metadata.annotations | to_entries | map(select(.key == "label1")) | from_entries'from_entries handles nicely the Tags in a lot of awscli output, you can do things like
aws ec2 describe-instances | \
jq '.Reservations[].Instances[] |
{Role: .Tags | from_entries | .role,
Name: .Tags | from_entries | .name,
Id: .InstanceId}' \
-C -c | sort | less -R
to get a summary of all your instances sorted by role.Give something like this a go if you know javascript: https://www.npmjs.com/package/jsling
I created jello[0], which uses python list and dict syntax to filter JSON. Here's a blog post[1] I wrote that shows how it can be used.
[0] https://github.com/kellyjonbrazil/jello [1] https://blog.kellybrazil.com/2020/03/25/jello-the-jq-alterna...
With that said this is a great overview!
s/rather than/in addition to/
For those of us with javascript disabled, the way the page works is perfectly fine as it is.
Do you have some small example where they do such an UI properly, so that i can copy it? I am not much of an expert in javascript/css.
I guess some of the examples would be simpler than the jq solution.
grep -Ev '({}|\[\])' | tr -d \; | cut -c 6-
It will remove useless cruft that is added for the output to be valid javascriptIt just makes more sense to do things in order even if it's a couple extra characters.
<input_file command_line
Why have a problem with this? Catting a single file is a well-known idiom for outputting its contents into a stream, plays well with positioning in pipelines, and has the nice property that you can erase as much of the pipeline as you want, to be able to peek inside it at any point.
Debian install instructions https://docs.microsoft.com/en-us/powershell/scripting/instal...
Enable https, add feed, apt install
As someone who started out scripting in Powershell but preferred Linux as an OS. I found myself missing powershell's object passing in opposition to bash's string passing.
That being said I now primarily use iPython for advanced shell tasks as I can leverage all of Python's libraries like JSON or YAML.
TBH they're both excellent choices. nushell looks really good too. The point is not to scrape text - rather than write a bash script that can handle JSON properly, write a pwsh/python/nushell script that handle everything properly (ie, by selecting fields rather than scraping text).
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-jessie-prod jessie main" > /etc/apt/sources.list.d/microsoft.list'
sudo apt-get update
sudo apt-get install -y powershell
I'm a node developer, here's what I used to find packages that had a cracked dependency recently: $results = @{}
ls -recurse -filter "eslint-scope" | foreach {
$file = "${PSItem}\package.json"
$version = cat $file | convertfrom-json | select -ExpandProperty version
$results.Add($file,$version) }
echo $results | format-list
PS. pwsh should really combine reading a file, determining it's type, and parsing it, into one command. Like: open $file | select -ExpandProperty version
I know I can make this, it just should be in the stdlib.> I don't want a better jq if it means relearning and remastering everything else along with it.
As I said, better jq may be equal to:
pwsh -Command "(cat test.json | ConvertFrom-Json).Whatever.Property.Or.Normal.Filter"