Docs.rs
  • jsonpath-rust-0.6.0
    • jsonpath-rust 0.6.0
    • Docs.rs crate page
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • besok
    • Dependencies
      • pest ^2.0 normal
      • pest_derive ^2.0 normal
      • regex ^1 normal
      • serde_json ^1.0 normal
      • thiserror ^1.0.50 normal
      • criterion ^0.5.1 dev
      • lazy_static ^1.0 dev
    • Versions
    • 34.29% of the crate is documented
  • Go to latest version
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate jsonpath_rust

jsonpath_rust0.6.0

  • All Items
  • Modules
  • Macros
  • Structs
  • Enums
  • Traits
  • Functions

Crates

  • jsonpath_rust
?
Settings

Crate jsonpath_rust

source ·
Expand description

§Json path

The library provides the basic functionality to find the slice of data according to the query. The idea comes from xpath for xml structures. The details can be found over there Therefore JSONPath is a query language for JSON, similar to XPath for XML. The jsonpath query is a set of assertions to specify the JSON fields that need to be verified.

§Simple example

Let’s suppose we have a following json:

 {
  "shop": {
   "orders": [
      {"id": 1, "active": true},
      {"id": 2 },
      {"id": 3 },
      {"id": 4, "active": true}
    ]
  }
}

And we pursue to find all orders id having the field ‘active’ we can construct the jsonpath instance like that $.shop.orders[?(@.active)].id and get the result [1,4]

§Another examples

{ "store": {
    "book": [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

and examples

  • $.store.book[*].author : the authors of all books in the store
  • $..book[?(@.isbn)] : filter all books with isbn number
  • $..book[?(@.price<10)] : filter all books cheapier than 10
  • $..* : all Elements in XML document. All members of JSON structure
  • $..book[0,1] : The first two books
  • $..book[:2] : The first two books

§Operators

  • $ : Pointer to the root of the json. It is gently advising to start every jsonpath from the root. Also, inside the filters to point out that the path is starting from the root.
  • @Pointer to the current element inside the filter operations.It is used inside the filter operations to iterate the collection.
  • * or [*]Wildcard. It brings to the list all objects and elements regardless their names.It is analogue a flatmap operation.
  • <..>| Descent operation. It brings to the list all objects, children of that objects and etc It is analogue a flatmap operation.
  • .<name> or .['<name>']the key pointing to the field of the objectIt is used to obtain the specific field.
  • ['<name>' (, '<name>')]the list of keysthe same usage as for a single key but for list
  • [<number>]the filter getting the element by its index.
  • [<number> (, <number>)]the list if elements of array according to their indexes representing these numbers. |
  • [<start>:<end>:<step>]slice operator to get a list of element operating with their indexes. By default step = 1, start = 0, end = array len. The elements can be omitted [:]
  • [?(<expression>)]the logical expression to filter elements in the list.It is used with arrays preliminary.

§Examples

 use std::str::FromStr;
 use serde_json::{json, Value};
 use jsonpath_rust::{jp_v, find_slice, JsonPathValue, JsonPathInst};

 fn test() -> Result<(), Box<dyn std::error::Error>> {
     let json = serde_json::from_str(r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#)?;
     let path = JsonPathInst::from_str("$.first.second[?(@.active)]")?;
     let slice_of_data:Vec<JsonPathValue<Value>> = find_slice(&path, &json);
     let js = json!({"active":1});
     assert_eq!(slice_of_data, jp_v![&js;"$.first.second[0]",]);
 }

Modules§

  • parser
    The parser for the jsonpath. The module grammar denotes the structure of the parsing grammar
  • path

Macros§

  • chain
  • filter
  • function
  • idx
  • jp_v
    just to create a json path value of data Example:
  • op
  • path

Structs§

  • JsonPathInst

Enums§

  • JsonPathValue
    A result of json path Can be either a slice of initial data or a new generated value(like length of array)
  • JsonPtr
    Json paths may return either pointers to the original json or new data. This custom pointer type allows us to handle both cases. Unlike JsonPathValue, this type does not represent NoValue to allow the implementation of Deref.

Traits§

  • JsonPathQuery
    the trait allows to mix the method path to the value of Value and thus the using can be shortened to the following one:

Functions§

  • find
    finds a slice of data and wrap it with Value::Array by cloning the data. Returns either an array of elements or Json::Null if the match is incorrect.
  • find_as_path
    finds a path of the values. If the values has been obtained by moving the data out of the initial json the path is absent.
  • find_slice
    finds a slice of data in the set json. The result is a vector of references to the incoming structure.

Results

trait
jsonpath_rust::path::Path
The trait defining the behaviour of processing every …
module
jsonpath_rust::path
trait method
jsonpath_rust::JsonPathQuery::path
enum variant
jsonpath_rust::parser::parser::Rule::path
macro
jsonpath_rust::path
type alias
jsonpath_rust::path::PathInstance
The basic type for instances.
method
jsonpath_rust::JsonPathValue::to_path
Transforms given value into path
enum
jsonpath_rust::parser::model::JsonPath
The basic structures for parsing json paths. The common …
struct
jsonpath_rust::JsonPathInst
extern crate
jsonpath_rust
Json path
enum
jsonpath_rust::parser::model::JsonPathIndex
trait
jsonpath_rust::JsonPathQuery
the trait allows to mix the method path to the value of …
enum
jsonpath_rust::JsonPathValue
A result of json path Can be either a slice of initial …
function
jsonpath_rust::path::json_path_instance
The major method to process the top part of json part
enum
jsonpath_rust::parser::errors::JsonPathParserError
function
jsonpath_rust::find_as_path
finds a path of the values. If the values has been …
function
jsonpath_rust::parser::parser::parse_json_path
Parses a string into a JsonPath.
method
jsonpath_rust::path::Path::needs_all
defines when we need to invoke find or flat_find
method
jsonpath_rust::path::Path::find
when every element needs to handle independently
method
jsonpath_rust::path::Path::flat_find
when the whole output needs to handle
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.