Expand description
§dot-prop — access nested JSON values by dot path
Get, set, check, and delete a property deep inside a serde_json::Value using a dot
path such as a.b.0.c or a[0].b. A faithful Rust port of the widely-used
dot-prop npm package.
use serde_json::json;
use dot_prop::{get_property, set_property, has_property, delete_property};
let mut value = json!({ "foo": { "bar": [10, 20] } });
assert_eq!(get_property(&value, "foo.bar.1"), Some(&json!(20)));
assert!(has_property(&value, "foo.bar.0"));
set_property(&mut value, "foo.bar.2", json!(30));
assert_eq!(get_property(&value, "foo.bar.2"), Some(&json!(30)));
delete_property(&mut value, "foo.bar.0");
assert_eq!(get_property(&value, "foo.bar.0"), Some(&json!(null)));Paths use . to separate keys, [i] for array/string indices, and \ to escape a
literal ., [, or \. Unlike json-pointer
(RFC 6901, /-separated), this matches the JavaScript dot-prop syntax.
Structs§
- Parse
Path Error - An error returned by
parse_pathfor a malformed path.
Enums§
- Segment
- A single component of a parsed path: an object key or an array index.
Functions§
- deep_
keys - Every leaf path of
object, as dot-path strings. - delete_
property - Delete the value at
path. Returns whether something was removed. - escape_
path - Escape
.,[, and\in a path segment so it is treated literally. - get_
property - Get the value at
path, orNoneif it is absent (or the path is malformed). - has_
property - Whether
pathexists inobject. - parse_
path - Parse a dot path into its segments.
- set_
property - Set
valueatpath, creating intermediate objects/arrays as needed. - stringify_
path - Build a dot path from segments. Equivalent to
stringify_path_withwithprefer_dot_for_indices = false. - stringify_
path_ with - Build a dot path from segments. When
prefer_dot_for_indicesis set, indices after the first segment are written as.0rather than[0]. - unflatten
- Expand a flat
{ "a.b": value }object into a nested value.