pub fn jsonb_object<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>, text_array>(
text_array: text_array,
) -> jsonb_object<Arr, text_array>where
text_array: AsExpression<Arr>,
Available on crate feature
postgres_backend
only.Expand description
Builds a JSON object out of a text array. The array must have an even number of members, in which case they are taken as alternating key/value pairs. This function also has a form that that takes keys and values as separate text array arguments. See [jsonb_object_with_keys_and_values]
ยงExample
let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world"]))
.get_result::<Value>(connection)?;
let expected:Value = serde_json::json!({"hello":"world"});
assert_eq!(expected, jsonb);
let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John","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::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
.get_result::<Option<Value>>(connection)?;
assert!(jsonb.is_none());
let empty:Vec<String> = Vec::new();
let jsonb = diesel::select(jsonb_object::<Array<Nullable<Text>>,_>(empty))
.get_result::<Value>(connection)?;
let expected = serde_json::json!({});
assert_eq!(expected, jsonb);
let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John"]))
.get_result::<Value>(connection);
assert!(jsonb.is_err());