json_extract_path_1

Function json_extract_path_1 

Source
pub fn json_extract_path_1<J: JsonOrNullableJson + SingleValue, T1: SingleValue, json, text_1>(
    json: json,
    text_1: text_1,
) -> json_extract_path_1<J, T1, json, text_1>
where json: AsExpression<J>, text_1: AsExpression<T1>,
Available on crate feature postgres_backend only.
Expand description

Extracts JSON sub-object at the specified path. (This is functionally equivalent to the #> operator, but writing the path out as a variadic list can be more convenient in some cases.)

§Example


let expected = Some(json!({"f5":99, "f6":"foo"}));
let result = diesel::select(json_extract_path_1::<Json, Text, _, _>(
    json!({"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}),
    "f4",
))
.get_result::<Option<serde_json::Value>>(connection)?;

assert_eq!(expected, result);

let expected = Some(json!("foo"));
let result = diesel::select(json_extract_path_2::<Json, Text, Text, _, _, _>(
    json!({"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}),
    "f4",
    "f6"
))
.get_result::<Option<serde_json::Value>>(connection)?;

assert_eq!(expected, result);

let result = diesel::select(json_extract_path_1::<Nullable<Json>, Text, _, _>(None::<serde_json::Value>, "f4"))
.get_result::<Option<serde_json::Value>>(connection)?;

assert_eq!(None::<serde_json::Value>, result);

let result = diesel::select(json_extract_path_2::<Json, Nullable<Text>, Text, _, _, _>(json!({"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}), None::<String>, "f6"))
.get_result::<Option<serde_json::Value>>(connection)?;


assert_eq!(None::<serde_json::Value>, result);

§Variadic functions

This function is variadic in SQL, so there’s a family of functions on a diesel side:

json_extract_path_0, json_extract_path_1, … json_extract_path_n

Here, the postfix number indicates repetitions of variadic arguments. To use this function, the appropriate version with the correct argument count must be selected.

§Controlling the generation of variadic function variants

By default, only variants with 0, 1, and 2 repetitions of variadic arguments are generated. To generate more variants, set the DIESEL_VARIADIC_FUNCTION_ARGS environment variable to the desired number of variants.

For a greater convenience this environment variable can also be set in a .cargo/config.toml file as described in the cargo documentation.