Jq: A lightweight and flexible command-line JSON processor
stedolan.github.io
stedolan.github.io
Thank you! When I clicked on the link I was fully expecting it to be written in Node and I was going to cry.
> You can download a single binary, scp it to a far away machine, and expect it to work.
But jq also has libjq, a C library. And jq uses a copy-on-write, reference counted representation of JSON values, which is, for example, inherently thread-safe (though jq isn't using atomic operations for refcount management yet). The library is easy to use and powerful.
Ultimately the main thing I love about jq (and why I contribute to it) is the jq language itself. It reminds me of my one-time favorite, Icon. But in a world where C is still the champion of systems programming (until Rust takes over?), it's real handy to have a C JSON library _and_ a functional DSL that's easy to invoke from C.
It's no secret either, just look at my commit messages' email address :)
It is a really cool tool. Especially since they use JSON internally for almost all of their data representations.
Jq + Percol[1] is kind of cool.
[1] https://github.com/mooz/percol - was on HN a few days ago.
A Unix-like shell with JSON for object serialization could approximate the PowerShell, no? Think of ksh93's awful compound variables... done right.
If you just need pretty printing and don't have jq installed, you can use the python command line with the built in JSON module:
wget http://reddit.com/.json | python -mjson.tool
wget -O- http://reddit.com/.json | python -mjson.tool
I find it nice to be able to loop the data, define new subsets, filter it with functions, etc.
It was a 10 minute slap-job to get it together, so it does barf on particularly large datasets. The obvious reason is that it serialises the JSON into an argument for a child process. Shouldn't be too hard to resolve if anyone feels like it.
https://github.com/davidbanham/dotfiles/commit/0ea950373604e...
% aws ec2 describe-instances --instance-ids '["i-3026a249","i-28739551"]' \
| jq '[.Reservations[].Instances[].InstanceId]'
[
"i-28739551",
"i-3026a249"
]EDIT: here is one I used. Given VOLUME_ID, gets the most recent snapshot-id.
SNAPSHOT_ID=$(\
aws ec2 describe-snapshots --owner-ids xxxxxxxxxxxx --output json |
jq -r "
.Snapshots | sort_by(.StartTime) | reverse[] |
select(.VolumeId==\"$VOLUME_ID\" and .State==\"completed\") |
.SnapshotId
")
EDIT2: now that I'm looking at it, I think that I expected it to work with multiple snapshots but I'm not sure that I tested it.http://www.w3.org/Tools/HTML-XML-utils/
However, I just use a Ruby script which loads the Nokogiri library:
https://github.com/clarkgrubb/data-tools/blob/master/src/dom...
https://github.com/clarkgrubb/data-tools/blob/master/doc/dom...
See this blog post I wrote a while ago for an example: http://jeroenjanssens.com/2013/09/19/seven-command-line-tool...
[ {"tag":"something", "attributes": { ... }, "nodes": [ ...] }, ... ]
with nodes being objects with one key to indicate if the node is a text node or an element, and if a text node then a value, and so on. jq 'reduce .[] as $obj ({}; .+$obj)'
or jq -s 'reduce .[] as $obj ({}; .+$obj)'
should do it. `+` adds objects by merging them.I've been thinking I need to do a 1.4.1 release just for that!
(Plus we're getting some cool new features suddenly. @wtlangford (on github) is working on regexps, for example. Maybe a 1.4.1 so soon would be justified.)
I have also used it to validate sudokus, because I can.
And just like that, you have an amazing base for diffing json documents of arbitrary complexity:
vimdiff <( cat doc1 | jq --sort-keys . ) <( cat doc2 | jq --sort-keys . )