pub fn jsonb_group_object<N: SqlType<IsNull = NotNull> + SingleValue, V: SqlType + SingleValue, names, values>(
names: names,
values: values,
) -> jsonb_group_object<N, V, names, values>where
names: AsExpression<N>,
values: AsExpression<V>,
Available on crate feature
sqlite
only.Expand description
The jsonb_group_object(NAME,VALUE) function returns a JSONB object comprised of all NAME/VALUE pairs in the aggregation.
A potential edge case in this function arises when names
contains duplicate elements.
In such case, the result will include all duplicates (e.g., {"key": 1, "key": 2, "key": 3}
).
Note that any duplicate entries in the resulting JSONB will be removed during deserialization.
This function requires at least SQLite 3.38 or newer
§Aggregate Function Expression
This function can be used as aggregate expression. See AggregateExpressionMethods
for details.
§Examples
§Normal function usage
let result = animals.select(jsonb_group_object(species, name)).get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"dog":"Jack","spider":null}), result);
§Aggregate function expression
let result = animals.select(jsonb_group_object(species, name).aggregate_filter(legs.lt(8))).get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"dog":"Jack"}), result);
§See also
json_group_object
will return data in JSON format instead of JSONB.jsonb_group_array
will return JSONB array instead of object.