json_dotpath

Access members of nested JSON arrays and objects using "dotted paths".

Consider this example JSON:

json { "fruit": [ {"name": "lemon", "color": "yellow"}, {"name": "apple", "color": "green"}, {"name": "cherry", "color": "red"} ] }

The following can be used to access its parts: - obj.dot_get("fruit") ... get the fruits array - obj.dot_get("fruit.0.name") ... 0th fruit name, "lemon" - obj.dot_get("fruit.>.color") ... last fruit's color, "red"

The JSON can also be manipulated:

Any serializable type or serde_json::Value can be stored to or retrieved from the nested object (Value::Object, Value::Array, Value::Null).

Any value stored in the object can also be modified in place, without deserialization, by getting a mutable reference (dot_get_mut(path)).

This crate is useful for tasks such as working with dynamic JSON API payloads, parsing config files, or building a polymorphic data store.

Supported Operations

Object and Array

Array

Array is an ordered sequence backed by a Vec. It has these additional operations:

Null

JSON null can become an array or object by setting it's members (even nested), as if it was an array or object. It becomes an array or object of the appropriate type based on the root key.

Dotted Path Syntax

Map Patterns

To avoid ambiguity, it's not allowed to use numeric keys (or keys starting with a number) as map keys. Map keys must start with an ASCII letter or underscore and must not contain a dot (.).

Examples:

If a numeric key or a key nonconforming in other way must be used, prefix it with #. It will be taken literally as a string, excluding the prefix.

e.g. to get 456 from {"foo":{"123":456}}, use foo.#123 instead of foo.123

Array Patterns

Array keys must be numeric (integer), or one of the special patterns listed below.

Path Examples

It's possible to create nested arrays or objects by setting a non-existent path, provided the key syntax rules are maintained.

See unit tests for more examples.