# jq Examples
Because I keep having to look at the [`jq` manual](https://jqlang.github.io/jq/manual/), here are some of my common examples for easy reference.
Some of these commands make use of my [`jq` modules](https://github.com/heaths/jq), assuming the repo is cloned 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:
```bash
# 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):
```bash=
// 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
```
Get the names of all dependencies from all crates in a workspace:
```bash
cargo metadata --format-version 1 \
| jq '[.packages[] | select(.id | startswith("path+file://")) | .dependencies[].name] | unique | sort'
```
### crates.io
Using my `version` module, you can get the latest crate info from <https://crates.io>:
```bash
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:
```bash
curl -s https://index.crates.io/az/ur/azure_core \
| jq -rs 'include "heaths/version"; [.[].vers] | max_by(toversion)'
```
You can also sort versions e.g., list the name, version, and rust-version (MSRV):
```bash
curl -s https://index.crates.io/az/ur/azure_core \
| jq -rs 'import "heaths/version" as v; [.[] | {name,vers,rust_version}] | sort_by(.vers | v::toversion) | reverse'
```
#### Downloads
If you want to get download counts for different versions of a crate sorted by version:
```bash
curl https://crates.io/api/v1/crates/azure_core \
| jq 'include "heaths/version"; [.versions[] | {crate, version: .num, created_at, downloads}] | sort_by(.version | toversion)'
```