-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add a value at a path? #18
Comments
if a value exist in a given path as above, you can use the replace_with function. fn add(
json: serde_json::Value,
path: &str,
key: &str,
value: serde_json::Value,
) -> Result<Option<serde_json::Value>, jsonpath::JsonPathError> {
let mut wrap = Box::new(value);
let mut selector = jsonpath::SelectorMut::new();
let ret = selector
.value(json)
.str_path(path)?
.replace_with(&mut |found| {
let mut ret = found.clone();
if let serde_json::Value::Object(ref mut map) = ret {
map.insert(key.to_string(), wrap.take());
}
ret
})?
.take();
Ok(ret)
}
fn main() -> Result<(), jsonpath::JsonPathError> {
let ret = add(
json!(
{
"a" : {
"b" : 1
}
}
),
"$.a",
"c",
json!(2),
)?;
println!("{:?}", ret);
Ok(())
} |
The thing is that we're getting a string path from the user and it's hard to remove the last part of the path so we can find the right place to add the new node. e.g. in this simple example we'll get |
gkorland
referenced
this issue
in gkorland/jsonpath
Aug 27, 2019
ref #18 exposing the parsing results will allow us to use the ParseTokens to add new paths
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g.
Add
$.a.c=2
to:
The text was updated successfully, but these errors were encountered: