pub fn json_group_object<N: SqlType<IsNull = NotNull> + SingleValue, V: SqlType + SingleValue, names, values>(
names: names,
values: values,
) -> json_group_object<N, V, names, values>where
names: AsExpression<N>,
values: AsExpression<V>,
Available on crate feature
sqlite
only.Expand description
The json_group_object(NAME,VALUE) function returns a JSON 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 JSON 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(json_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(json_group_object(species, name).aggregate_filter(legs.lt(8))).get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"dog":"Jack"}), result);
§See also
jsonb_group_object
will return data in JSONB format instead of JSON.json_group_array
will return JSON array instead of object.