Skip to main content

json_get

Function json_get 

Source
pub fn json_get<'a, E>(
    expr: E,
    key: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, <E::SQLType as JsonType>::Field, Null, E::Aggregate, E::Sources>
where E: Expr<'a, PostgresValue<'a>>, E::SQLType: JsonType,
Expand description

Gets an object field by key (->), keeping the JSON type.

The operand must be json or jsonb (JsonType); the result has the same type, so calls can be chained. The result is NULL when the key is missing.

ยงExamples

use drizzle_postgres::expr::json_get;
use drizzle_core::{ToSQL, expr::raw_non_null};
use drizzle_postgres::values::PostgresValue;
use drizzle_types::postgres::types::Jsonb;

let data = raw_non_null::<PostgresValue, Jsonb>("data");
let address = json_get(data, "address"); // a `jsonb` expression
assert_eq!(address.to_sql().sql(), "data -> CAST ($1 AS TEXT)");