6 releases (3 breaking)
Uses new Rust 2024
| 0.3.1 | Oct 30, 2026 |
|---|---|
| 0.3.0 | Oct 30, 2026 |
| 0.2.0 | Oct 5, 2026 |
| 0.1.1 | Oct 4, 2026 |
| 0.0.0 | May 24, 2025 |
#1855 in Template engine
14,309 downloads per month
Used in 11 crates
66KB
1K
SLoC
jq.rs
jq.rs brings jq-style JSON filtering to Rust.
[Features] | [Prerequisites] | [Installation] | [Examples] | [Reference] | [Development]
✨ Features
- Compile filters once and reuse them with the in-process jaq backend.
- Work with JSON strings or
serde_json::Valueinputs. - Return the first result, collect all results, or visit results incrementally.
- Bind named JSON variables and supply auxiliary inputs to
inputandinputs. - Choose jaq or the upstream jq executable through Cargo feature flags.
- Supports
no_stdwith heap allocation. - Cuts red tape: 100% free and unencumbered public domain software.
🛠️ Prerequisites
⬇️ Installation
Install the latest release from Crates.io:
cargo add jq serde_json
For the development version documented here, including named bindings and the subprocess backend:
cargo add jq --git https://github.com/dryrust/jq.rs
cargo add serde_json
👉 Examples
Filtering JSON
Parse a filter once and reuse it. Choose the first result or collect them all:
use jq::{JsonFilter, JsonFilterError};
use serde_json::json;
fn main() -> Result<(), JsonFilterError> {
let filter: JsonFilter = ".items[] | .name".parse()?;
let input = r#"{"items": [{"name": "Ada"}, {"name": "Lin"}]}"#;
assert_eq!(
filter.filter_json_str(input)?,
json!("Ada"),
);
assert_eq!(
filter.filter_json_str_all(input)?,
[json!("Ada"), json!("Lin")],
);
Ok(())
}
Binding Variables
Pass values as named variables without interpolating them into filter source:
use jq::{JsonFilter, JsonFilterError};
use serde_json::json;
fn main() -> Result<(), JsonFilterError> {
let mut filter = JsonFilter::with_bindings(
".[] | select(. >= $minimum)",
[("minimum", json!(10))],
)?;
assert_eq!(
filter.filter_json_all(json!([5, 12, 20]))?,
[json!(12), json!(20)],
);
filter.set_bindings([("minimum", json!(15))])?;
assert_eq!(filter.filter_json_all(json!([5, 12, 20]))?, [json!(20)]);
Ok(())
}
📚 Reference
Rust API
JsonFilter compiles and evaluates filters; JsonFilterError reports
failures. See the API documentation for method contracts and backend behavior,
or run cargo doc --open for the development version.
Feature Flags
| Feature | Purpose |
|---|---|
jaq |
In-process Rust backend. |
jq |
Upstream jq subprocess backend; enables std. |
std |
Standard-library support and jaq's optional functions. |
env |
Exposes host environment variables to env; enables std. |
import |
Lets the jq backend load modules and JSON data files. |
log |
Forwards jaq's stderr and debug messages to log. |
all |
Enables both backends. |
Defaults enable all and std. env and import are opt-in, as they
expose secrets in environment variables and files; never enable them for
untrusted programs. jaq takes precedence when both backends are
enabled. To select the subprocess backend, set default-features = false and
features = ["jq"] on the dependency. For no_std with heap allocation, use
default-features = false and features = ["jaq"] instead.
Both backends use the same Rust API, but their jq language behavior can differ.
👨💻 Development
git clone https://github.com/dryrust/jq.rs.git
This README was generated with Readmer.
Dependencies
~0–2.1MB
~35K SLoC