Skip to content

Instantly share code, notes, and snippets.

@heaths
Last active January 17, 2025 20:50
Show Gist options
  • Save heaths/fc161d6f5a59da7742218229fbd2a9dc to your computer and use it in GitHub Desktop.
Save heaths/fc161d6f5a59da7742218229fbd2a9dc to your computer and use it in GitHub Desktop.
jq examples

Because I keep having to look at the jq manual, here are some of my common examples for easy reference.

Some of these commands make use of my jq modules, assuming the repo is clonsed to a heaths directory in the default or specified module root e.g., ~/.jq/heaths.

Rust

Select all instances of a dependency from cargo metadata in a workspace and put them into a JSON array:

# Alternatively could put the whole first jq expression into square brackets but I find this easier to modify the expression.
cargo metadata --format-version 1 | jq '.packages[] as $package | $package.dependencies[] | select(.name == "azure_core") | {name: .name, req: .req, package: $package.name}' | jq -s

Publish all packages in a workspace (pass -n to cargo publish for a dry run):

// Any dependencies like azure_core should be listed before dependents.
for p in $(cargo metadata --format-version 1 | jq -r '.packages[] | select(.name | startswith("azure")) | .name'); do
  cargo publish --package $p
done

crates.io

Using my version module, you can get the latest crate info from https://crates.io:

curl -s https://index.crates.io/az/ur/azure_core | jq -s 'include "heaths/version"; . | max_by(.vers | toversion)'

Or just to get the version number:

curl -s https://index.crates.io/az/ur/azure_core | jq -rs 'include "heaths/version"; [.[].vers] | max_by(toversion)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment