#json #json-path #sql

bin+lib sql-json-path

SQL/JSON Path implementation in Rust

1 unstable release

0.1.0 Nov 29, 2023

#297 in Data structures

Download history 3730/week @ 2023-12-14 4616/week @ 2023-12-21 3233/week @ 2023-12-28 4916/week @ 2024-01-04 4744/week @ 2024-01-11 5671/week @ 2024-01-18 4809/week @ 2024-01-25 6083/week @ 2024-02-01 1409/week @ 2024-02-08 1672/week @ 2024-02-15 4502/week @ 2024-02-22 3839/week @ 2024-02-29 3562/week @ 2024-03-07 4074/week @ 2024-03-14 3607/week @ 2024-03-21 3551/week @ 2024-03-28

15,329 downloads per month

Apache-2.0

105KB
2.5K SLoC

sql-json-path

Build status Crate Docs

SQL/JSON Path implementation in Rust.

Features

  • Compatible with SQL/JSON Path standard and PostgreSQL implementation.
  • Independent from JSON implementation. It supports popular libraries like serde_json, simd-json and jsonbb. Custom JSON types are also supported.

Usage

use serde_json::{json, Value};
use sql_json_path::JsonPath;

let json = json!({"a": 1});
let path = JsonPath::new("$.a").unwrap();

let nodes = path.query(&json).unwrap();
assert_eq!(nodes.len(), 1);
assert_eq!(nodes[0].to_string(), "1");

JSON Path Syntax

See PostgreSQL documentation for more details.

  • strict and lax mode
  • $: Root object
  • @: Current object
  • [index]: An array element by index
    • [start to end]: An array slice
    • [index1, index2, ...]: Multiple array elements
    • [last]: The last array element
  • .name: An object member by name
  • [*]: Any array element
  • .*: Any object member
  • .**: Any descendant object member (Postgres extension)
  • ?(predicate): Filter expression
    • ==, != / <>, <, <=, >, >=: Comparison
    • &&, ||, !: Logical operators
    • is unknown: Check if the value is unknown
    • like_regex: Check if the string matches the regular expression
    • starts with: Check if the string starts with the given prefix
    • exists(expr): Check if the expression matches any value
  • Operations
    • +: Addition / Unary plus
    • -: Subtraction / Negation
    • *: Multiplication
    • /: Division
    • %: Modulo
  • Methods
    • .type()
    • .size()
    • .double()
    • .ceiling()
    • .floor()
    • .abs()
    • .datetime()
    • .datetime(template)
    • .keyvalue()

Testing

This crate is tested against PostgreSQL regression tests: cargo test --test pg_jsonb_jsonpath. 325 out of 430 tests are passed. 96 tests are skipped because they have unsupported features. 9 tests are failed because of incorrect implementation.

License

Licensed under Apache License, Version 2.0.

Dependencies

~3.5–6.5MB
~121K SLoC