jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

How to delete the key of a path?

Open gkorland opened this issue 6 years ago • 3 comments

e.g.

{
   "a" : {
              "b": 1,
              "c": 2
          }
}

delete $.a.c including removing c and not setting NULL in c

to

{
   "a" : {
              "b": 1
          }
}

gkorland avatar Jul 03 '19 08:07 gkorland

delete function do not delete key but replace with null.

let mut selector_mut = SelectorMut::default();
let ret: Value = selector_mut
    .value(json!({
        "a": {
            "b": 1,
            "c": 2
        }
    }))
    .str_path("$.a.c").unwrap()
    .delete().unwrap()
    .take().unwrap();

assert_eq!(ret, json!({
    "a": {
            "b": 1,
            "c": null
        }
}));

freestrings avatar Jul 07 '19 07:07 freestrings

Yes, I know. I'm asking how can I achieve the key delete?

gkorland avatar Jul 07 '19 07:07 gkorland

default operation do not delete key. because i prefer to null value instead of missing key. if you prefer to key deletion, need to optional parameter and conditional implementation. eg SelectorMut::new( option ).

freestrings avatar Jul 08 '19 01:07 freestrings