jetro
Jetro is a library which provides a custom DSL for transforming, querying and comparing data in JSON format. It is easy to use and extend.
Jetro has minimal dependency, the traversal and eval algorithm is implemented on top of serde_json.
Jetro can be used inside Web Browser by compiling down to WASM. Clone it and give it a shot.
Jetro can be used in command line using Jetrocli.
Jetro combines access paths with functions that operate on values matched within the pipeline. Access paths use / as separator (similar to URI structure). The start of a path denotes the traversal root:
>— traverse from document root<— traverse from root in nested paths@— current pipeline item (used inside construction literals and#map)
Expressions support line breaks and whitespace; statements can be broken into multiple lines. Functions are denoted with the # operator and can be composed.
Language Overview
Path Navigation
| Syntax | Description |
|---|---|
>/key |
Child field access |
>/* |
Any child (wildcard) |
>/..key |
Recursive descendant search |
>/..('k'='v') |
Descendant where field equals value |
>/('a' | 'b') |
First matching key among alternatives |
>/[n] |
Array index access |
>/[n:m] |
Array slice |
>/[:n] |
Array from start up to index n |
>/[n:] |
Array from index n to end |
Construction Literals
Jetro expressions can construct new JSON objects and arrays inline. Each field value is itself a Jetro expression.
Object construction:
>{ "key": expression, [dynamic_key_expr]: expression, ... }
Keys can be static string literals (single- or double-quoted) or dynamic expressions wrapped in [...].
Array construction:
>[ expression, expression, ... ]
@ current-item path refers to the current pipeline element and is usable inside construction literals and #map:
>/items/#map(>{ "label": @/name, "price_incl_tax": @/price })
Functions
Existing functions
| Function | Description |
|---|---|
#pick('key' | expr, ...) |
Select keys / sub-expressions to build a new object |
#head |
First element of an array |
#tail |
All elements except the first |
#last |
Last element of an array |
#nth(n) |
Element at index n |
#keys |
Keys of an object |
#values |
Values of an object |
#reverse |
Reverse an array |
#min |
Minimum numeric value |
#max |
Maximum numeric value |
#sum |
Sum of numeric values |
#len |
Length of array or object |
#all |
True when all boolean values are true |
#any |
True when at least one boolean value is true |
#not |
Logical negation of a boolean value |
#zip |
Zip two or more arrays into an array of objects |
#map(x: x.field) |
Map each element through a path expression |
#filter('field' op value [and|or ...]) |
Filter an array by condition |
#formats('{} {}', 'k1', 'k2') [-> | ->* 'alias'] |
Format a string from field values |
Math functions
| Function | Description |
|---|---|
#avg |
Average of numeric values |
#add(n) |
Add scalar n to each numeric value |
#sub(n) |
Subtract scalar n from each numeric value |
#mul(n) |
Multiply each numeric value by n |
#div(n) |
Divide each numeric value by n |
#abs |
Absolute value |
#round |
Round to nearest integer |
#floor |
Round down |
#ceil |
Round up |
Array transforms
| Function | Description |
|---|---|
#flatten |
Flatten one level of nested arrays |
#flat_map(x: x.field) |
Map then flatten |
#chunk(n) |
Split array into chunks of size n |
#unique |
Remove duplicate scalar values |
#distinct('key') |
Remove duplicate objects by a key field |
#sort_by('key' [, 'desc']) |
Sort objects by a field (ascending by default) |
#join_str('sep') |
Join string values with a separator |
#compact |
Remove null values from an array |
#count |
Count of elements (alias for #len) |
Grouping & indexing
| Function | Description |
|---|---|
#group_by('key') |
Group objects into { "value": [...] } |
#count_by('key') |
Count objects per distinct key value |
#index_by('key') |
Index objects into { "value": object } |
#tally |
Count occurrences of each scalar value |
Object manipulation
| Function | Description |
|---|---|
#merge |
Merge an array of objects into one |
#omit('key') |
Remove a key from an object |
#select('key') |
Keep only the given key in an object |
#rename('old', 'new') |
Rename a key |
#set('key', 'value') |
Set or overwrite a key with a static value |
#coalesce('default') |
Return default if the value is null |
#get('key') |
Extract a single field from an object in a pipeline |
Join & lookup
| Function | Description |
|---|---|
#join(>/other, 'left_key', 'right_key') |
Inner join: merge each left object with its match from right |
#lookup(>/other, 'left_key', 'right_key') |
Left join: merge left with first match from right (null-patch on miss) |
Field resolution
These functions resolve references between collections — like foreign-key lookups in a query pipeline. They work both within a single document and across nodes in a Graph.
| Function | Description |
|---|---|
#find(condition) |
First element in array matching a filter condition |
#filter_by(condition) |
All elements matching a filter condition |
#pluck('key') |
Extract one field from every object in an array |
#resolve('ref', >/target [, 'match']) |
Replace each item's reference field with the full matched object |
#deref(>/target [, 'match']) |
Current value IS the reference; return the matched object |
#find / #filter_by accept the same condition syntax as #filter:
>/items/#find('type' == 'ingredient')
>/items/#filter_by('price' > 2.0 and 'is_gratis' == false)
#resolve — for each object in the input array, look up the value of ref_field in the target collection and replace the scalar reference with the full matched object:
>/orders/#resolve('customer_id', >/customers, 'id')
#deref — the current value is itself the reference key; returns the first matched object:
>/order/customer_id/#deref(>/customers, 'id')
Graph — multi-document queries
Graph lets you register named JSON documents and query across all of them. All nodes are merged into a virtual root { "node_name": value, ... } that any Jetro expression can navigate.
use Graph;
let mut g = new;
g.add_node;
g.add_node;
// Query the virtual merged root
let result = g.query?;
// Query a specific node only
let result = g.query_node?;
// Build a message schema — values are expressions evaluated against the graph
let result = g.message?;
Quick-start example
let data = json!;
let mut values = collect;
let output: = values.from_index;
Example dataset
The following JSON is used in the query examples below.
Queries
Get value associated with line_items.
>/line_items
Get value associated with first matching key which has a value and return its id field.
>/('non-existing-member' | 'customer')/id
"xyz"
Recursively search for objects that have a key with a specified value.
>/..('type'='ingredient')
Tail of the items list.
>/..items/#tail
Filter with compound condition.
>/..items/#filter('is_gratis' == true and 'name' ~= 'ChILLi')
Filter then map.
>/..items/#filter('is_gratis' == true and 'name' ~= 'ChILLi')/#map(x: x.type)
Construct a summary object.
>/#pick(
>/..line_items/*/#filter('is_gratis' == false)/..price/#sum as 'total',
>/..user/profile/#formats('{} {}', 'firstname', 'lastname') ->* 'fullname'
)
Slice the first four items.
>/..items/[:4]
Select from the fourth index to end.
>/..items/[4:]
Count gratis items.
>/#pick(>/..items/..is_gratis/#len as 'total_gratis')
Keys and values of the first item.
>/..items/[0]/#keys
>/..items/[0]/#values
Zip two or more arrays together.
>/#pick(>/..name as 'name', >/..nested as 'field', >/..b as 'release')/#zip
Result:
Group items by type, then count.
>/..items/#group_by('type')
>/..items/#count_by('type')
Sort items by price descending, then pluck their names.
>/..items/#sort_by('price', 'desc')/#pluck('name')
Construct a new object for each item using @ (current-item path) inside an object literal.
>/..items/#map(>{ "label": @/name, "unit_price": @/price, "gratis": @/is_gratis })
Find the first ingredient and resolve it — example with a separate catalogue document.
let catalogue = json!;
let mut g = new;
g.add_node;
g.add_node;
// Resolve each item's ident against the catalogue
let result = g.query?;
Example
The following dataset models a B2B SaaS company's operational data — customers, product catalogue, sales orders, line items, and employees — all in a single document. Every Jetro feature is demonstrated against it.
Path navigation
Company name (simple child access):
>/company/name
"Arctiq Systems Inc."
All customer contact emails (recursive descendant):
>/customers/..email
First matching top-level key (alternatives):
>/('billing_contact' | 'company')/name
"Arctiq Systems Inc."
Recursive search for all pending orders:
>/..('status'='pending')
First three orders (slice):
>/orders/[:3]
Aggregation & math
Total pipeline value across all orders:
>/orders/..subtotal/#sum
49450.0
Average monthly recurring revenue:
>/customers/..mrr/#avg
7998.0
Largest single order value:
>/orders/..total/#max
27275.0
Apply 10% enterprise discount to each product price:
>/products/#map(>{ "name": @/name, "discounted": @/unit_price })
(pair with #mul(0.9) per field in a full pipeline)
Filtering
All closed-won orders:
>/orders/#filter('status' == 'closed_won')
Enterprise customers in NA region:
>/customers/#filter('tier' == 'enterprise' and 'region' == 'NA')
First active enterprise customer (#find):
>/customers/#find('tier' == 'enterprise')
All customers tagged as regulated (#filter_by):
>/customers/#filter_by('tier' == 'mid-market')
Grouping & counting
Orders grouped by status:
>/orders/#group_by('status')
Count orders per status:
>/orders/#count_by('status')
Customer tier distribution (#tally on plucked values):
>/customers/#pluck('tier')/#tally
Index customers by ID for O(1) lookup:
>/customers/#index_by('id')
Sorting & extracting
Products sorted by unit price descending:
>/products/#sort_by('unit_price', 'desc')
All customer names (pluck):
>/customers/#pluck('name')
Distinct billing models across all products:
>/products/#pluck('billing')/#unique
Orders chunked into batches of 2:
>/orders/#chunk(2)
Construction literals
Build an executive summary object from expressions:
>{
"company": >/company/name,
"total_customers": >/customers/#len,
"active_mrr": >/customers/#filter('active' == true)/..mrr/#sum,
"open_pipeline": >/orders/#filter('status' == 'pending')/..total/#sum
}
Build a flat array of employee names:
>[ >/employees/[0]/name, >/employees/[1]/name, >/employees/[2]/name ]
Reshape each order using @ current-item path:
>/orders/#map(>{
"order_ref": @/id,
"customer": @/customer_id,
"rep": @/rep_id,
"value": @/total,
"outcome": @/status
})
Object manipulation
Strip internal tags from a customer record:
>/customers/[0]/#omit('tags')
Keep only the contract sub-object:
>/customers/[0]/#select('contract')
Rename mrr to monthly_revenue in each contract:
>/customers/..contract/#rename('mrr', 'monthly_revenue')
Merge all employee records into one object:
>/employees/#map(>{ [>/employees/#pluck('id')]: @/name })/#merge
Remove null min_seats from every product:
>/products/#map(>{ "id": @/id, "name": @/name, "price": @/unit_price, "billing": @/billing })
Join & lookup
Enrich every order with its full customer record (inner join on id):
>/orders/#join(>/customers, 'customer_id', 'id')
Left-join orders to customers (null-patched on miss):
>/orders/#lookup(>/customers, 'customer_id', 'id')
Field resolution
Resolve customer_id to a full customer object on every order (#resolve):
>/orders/#resolve('customer_id', >/customers, 'id')
Dereference a specific order's customer ID to a full object (#deref):
>/orders/[0]/customer_id/#deref(>/customers, 'id')
Extract all line-item product IDs from a single order:
>/orders/[0]/line_items/#pluck('product_id')
Flatten & zip
Collect every line item across all orders into a single flat array:
>/orders/#map(>[ @/line_items ])/#flatten
Zip rep names with their quotas:
>/#pick(>/employees/#pluck('name') as 'rep', >/employees/#pluck('quota') as 'quota')/#zip
#pick with sub-expressions
Build a full revenue dashboard in a single expression:
>/#pick(
>/orders/#filter('status' == 'closed_won')/..total/#sum as 'closed_revenue',
>/orders/#filter('status' == 'pending')/..total/#sum as 'pipeline_value',
>/customers/#filter('tier' == 'enterprise')/..mrr/#sum as 'enterprise_mrr',
>/customers/#len as 'total_accounts',
>/orders/#filter('status' == 'closed_won')/#len as 'deals_closed'
)
Graph — cross-document queries
Load orders and customers as separate Graph nodes and query across them without embedding one document inside the other.
use Graph;
let mut g = new;
g.add_node; // the "orders" array above
g.add_node; // the "customers" array above
g.add_node; // the "products" array above
// Revenue per customer tier — join happens across node boundaries
let result = g.query?;
// Build a report schema — each value is a Jetro expression evaluated
// against the merged virtual root
let report = g.message?;
Architecture
Jetro consists of three layers:
- Parser (
src/parser.rs) — PEG grammar (grammar.pest) parsed with pest, produces aVec<Filter>. - Context (
src/context.rs) — stack-based depth-first evaluator; eachStackItemcarries the current value and the remaining filter tail. - Functions (
src/func.rs) — pluggableCallabletrait;FuncRegistrymaps names to implementations. The default registry includes all built-in functions. - Graph (
src/graph.rs) — multi-document virtual root; all named nodes are merged so cross-document expressions work without any special syntax.