Crates.io | jqesque |
lib.rs | jqesque |
version | 0.0.2 |
source | src |
created_at | 2024-10-19 18:24:33.958701 |
updated_at | 2024-10-30 12:24:01.664545 |
description | Simple string assignment expressions with JQ-esque syntax for JSON interaction |
homepage | |
repository | https://github.com/terjekv/jqesque |
max_upload_size | |
id | 1415630 |
size | 61,787 |
A Rust library to parse simplified JSON assignments in a jq-like syntax and convert them into JSON structures.
Sometimes you want to express simplified JSON assignments as strings without writing the full JSON syntax. This library borrows syntax from jq and JSONPath to create a simplified way to represent JSON assignments.
foo.bar.baz=true
).foo[0].bar=zoot
, where the index must be a positive number).Separator::Dot
(.
), Separator::Slash
(/
), or Separator::Custom(char)
(custom character).Values can be anything that serde_json can parse, including strings, numbers, booleans, null, objects, and arrays.
The syntax is inspired by jq and JSONPath RFC9535 and is as follows:
[<operation>]<path>=[<value>]
<operation>
: An optional operation to perform. Supported operations are Add (+), Replace (=), Remove (-), Test (?), Insert (>), and Merge (~).<path>
: The path to the JSON key. The path can be nested and can include array indices. The path can be separated by a dot (.
), a slash (/
), or a custom character.<value>
: A JSON value. Note that the Remove operation does not require a value.Add, Remove, Replace, and Test operations are done as per the JSON Patch specification in RFC6902.
For more information, see the Operation enum itself.
Paths can be nested and can include array indices. The path can be separated by a dot (.
), a slash (/
), or a custom character.
Values are parsed by serde_json. The library will attempt to parse the value as a JSON value, defaulting to string.
use jqesque::Jqesque;
use serde_json::json;
fn main() {
let input = ">foo.bar[0].baz=hello";
let jqesque = input.parse::<Jqesque>().unwrap();
// Without using turbofish syntax:
// let jqesque: Jqesque = input.parse().unwrap();
// Alternatively, if you want to specify the separator:
// let jqesque = Jqesque::from_str_with_separator(input, Separator::Dot).unwrap();
let json_output = jqesque.as_json();
assert_eq!(json_output, json!({
"foo": {
"bar": [
{
"baz": "hello"
}
]
}
}));
}
use jqesque::{Jqesque, Separator};
use serde_json::json;
fn main() {
let input = ">foo/bar[0]/baz=true";
let jqesque = Jqesque::from_str_with_separator(input, Separator::Slash).unwrap();
let json_output = jqesque.as_json();
assert_eq!(json_output, json!({
"foo": {
"bar": [
{
"baz": true
}
]
}
}));
}
use serde_json::json;
use jqesque::{Jqesque, Separator};
let mut json_obj = json!({
"settings": {
"theme": {
"color": "red",
"font": "Arial",
"size": 12
}
}
});
let input = ">settings.theme={\"color\":\"blue\",\"font\":\"Helvetica\"}";
let jqesque = Jqesque::from_str_with_separator(input, Separator::Dot).unwrap();
jqesque.apply_to(&mut json_obj);
let expected = json!({
"settings": {
"theme": {
"color": "blue",
"font": "Helvetica"
}
}
});
assert_eq!(json_obj, expected);
// Note that the "size" key in the original "theme" object is removed.
use serde_json::json;
use jqesque::{Jqesque, Separator};
let mut json_obj = json!({
"settings": {
"theme": {
"color": "red",
"font": "Arial",
"size": 12
}
}
});
let input = "~settings.theme={\"color\":\"blue\",\"font\":\"Helvetica\"}";
let jqesque = Jqesque::from_str_with_separator(input, Separator::Dot).unwrap();
jqesque.apply_to(&mut json_obj);
let expected = json!({
"settings": {
"theme": {
"color": "blue",
"font": "Helvetica",
"size": 12
}
}
});
assert_eq!(json_obj, expected);
// Note that the "size" key in the original "theme" object is preserved.
See the LICENSE file for details.