Skip to main content

Crate dot_prop

Crate dot_prop 

Source
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§

ParsePathError
An error returned by parse_path for 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, or None if it is absent (or the path is malformed).
has_property
Whether path exists in object.
parse_path
Parse a dot path into its segments.
set_property
Set value at path, creating intermediate objects/arrays as needed.
stringify_path
Build a dot path from segments. Equivalent to stringify_path_with with prefer_dot_for_indices = false.
stringify_path_with
Build a dot path from segments. When prefer_dot_for_indices is set, indices after the first segment are written as .0 rather than [0].
unflatten
Expand a flat { "a.b": value } object into a nested value.