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. Visit Jetro Web to try it online, or clone it and give it a shot.
Jetro can be used in command line using Jetrocli.
Jetro combines access path with functions which operate on values matched within the pipeline. Access path uses /
as separator similar to structure of URI, the start of access path should denote whether the access starts from root by using >
, it is possible to traverse from root in nested paths by using <
.
By convention, functions are denoted using #
operator. Functions can be composed.
| Function | Action | | -------- | ------ | | #pick('string' \| expression, ...) [ as \| as* 'bindingvalue' ] | Select a key from an object, bind it to a name, select multiple sub queries to create new object | | #head | Head of the list| | #tail | Tail of the list | | #reverse | Reverse the list | | #all | Whether all boolean values are true | | #sum | Sum of numbers | | #formats('format with placeholder {} {}', 'keya', 'keyb') [ -> \| ->* 'bindingvalue' ] | Insert formatted key:value into object or return it as single key:value | | #filter('targetkey' (>, <, >=, <=, ==, ~=, !=) (string, boolean, number)) | Perform Filter on list | | #map(x: x.y.z \| x.y.z.somemethod())| Map each item in a list with the given lambda |
```rust let data = serdejson::json!({ "name": "mr snuggle", "someentry": { "someobj": { "obj": { "a": "objecta", "b": "objectb", "c": "objectc", "d": "object_d" } } } });
let mut values = Path::collect(data, ">/..obj/#pick('a','b')");
struct Output { a: String, b: String, }
let output: Option
Jetro consists of a parser, context wrapper which manages traversal and evaluation of each step of user input and a runtime for dynamic functions. The future version will support user-defined functions.
json
{
"bar": {
"meows": [
10,
20,
30,
40,
50,
60
],
"person": {
"firstname": "Mio",
"lastname": "Snuggle",
"hasFurr": true
},
"rolls": [
{
"roll": "on side"
},
{
"roll": "in space"
},
{
"roll": "in multiverse"
},
{
"roll": "everywhere"
}
]
},
"foo": [
1,
2,
3,
4,
5,
{
"contract": {
"kind": "Furry Purr",
"hasFurr": false
}
}
],
"friend": "Thunder Pur"
}
Get value associated with bar
.
```
/bar ```
See output
### result
json
"bar": {
"meows": [
10,
20,
30,
40,
50,
60
],
"person": {
"firstname": "Mio",
"lastname": "Snuggle"
}
}
Get value associated with bar
, return first value associated with any existing key.
```
/bar/('person' | 'whatever') ```
See output
### result
json
{
"firstname": "Mio",
"hasFurr": true,
"lastname": "Snuggle"
}
```
/('foo' | 'bar' | 'non-exinsting-key') ```
See output
### result
json
[
1,
2,
3,
4,
5,
{
"contract": {
"kind": "Furry Purr"
}
}
]
```
/('foo' | 'bar' | 'non-exinsting-key')/[:5]/#sum ```
See output
### result
json
15
```
/..rolls/#filter('roll' == 'everywhere') ```
See output
### result
json
[
{
"roll": "everywhere"
}
]
```
/..rolls/#filter('priority' < 11 and 'roll' ~= 'ON Side') ```
See output
### result
json
[
{
"priority": 1,
"roll": "on side"
}
]
```
/..rolls/#head/#formats('Rolling {}', 'roll') -> 'msg' ```
See output
### result
json
{
"msg": "Rolling on side",
"priority": 1,
"roll": "on side"
}
```
/..foo/[:4] ```
See output
### result
json
[
1,
2,
3,
4
]
```
/..meows/[4:] ```
See output
### result
json
[
50,
60
]
```
/#pick(>/..hasFurr/#all as 'totallyFurry') ```
See output
### result
json
{
"totallyFurry": true
}
```
/#pick('foo', >/..person/#formats('Herrn {} {}', 'firstname', 'lastname') ->* 'fullname') ```
See output
### result
json
{
"foo": [
1,
2,
3,
4,
5,
{
"contract": {
"kind": "Furry Purr"
}
}
],
"fullname": "Herrn Mio Snuggle"
}
```
/..foo/..contract/#pick('kind' as 'contract', * 'welcome_message') ```
See output
### result
```json { "contract": "Furry Purr", "welcome_message": "Welcome Mio" }
```
```
/..values/#map(x: x.name)/#head ```
See output
### result
json
"gearbox"