diesel::pg::expression::dsl

Function jsonb_object_with_keys_and_values

Source
pub fn jsonb_object_with_keys_and_values<Arr1: TextArrayOrNullableTextArray + SingleValue, Arr2: TextArrayOrNullableTextArray + CombinedNullableValue<Arr1, Jsonb>, keys, values>(
    keys: keys,
    values: values,
) -> jsonb_object_with_keys_and_values<Arr1, Arr2, keys, values>
where keys: AsExpression<Arr1>, values: AsExpression<Arr2>,
Available on crate feature postgres_backend only.
Expand description

This form of jsonb_object takes keys and values pairwise from two separate arrays. In all other respects it is identical to the one-argument form.

ยงExample

let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
            vec!["hello","John"],vec!["world","Doe"]))
            .get_result::<Value>(connection)?;
let expected:Value = serde_json::json!({"hello":"world","John":"Doe"});
assert_eq!(expected, jsonb);

let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Nullable<Array<Text>>, Nullable<Array<Text>>, _, _>(
            Some(vec!["hello","John"]),None::<Vec<String>>))
            .get_result::<Option<Value>>(connection)?;
assert_eq!(None::<Value>,jsonb);

let empty: Vec<String> = Vec::new();
let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
            vec!["hello","John"],empty))
            .get_result::<Value>(connection);
assert!(jsonb.is_err());