diesel::pg::expression::dsl

Function json_object_with_keys_and_values

source
pub fn json_object_with_keys_and_values<Arr1: TextArrayOrNullableTextArray + SingleValue, Arr2: TextArrayOrNullableTextArray + CombinedNullableValue<Arr1, Json>, keys, values>(
    keys: keys,
    values: values,
) -> json_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 json_object takes keys and values pairwise from two separate arrays. In all other respects it is identical to the one-argument form.

ยงExample

let json = diesel::select(json_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,json);

let json = diesel::select(json_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>,json);

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