diesel::pg::expression::dsl

Function json_array_length

Source
pub fn json_array_length<E: JsonOrNullableJson + MaybeNullableValue<Integer>, json>(
    json: json,
) -> json_array_length<E, json>
where json: AsExpression<E>,
Available on crate feature postgres_backend only.
Expand description

Returns the number of elements in the top-level JSON array

ยงExample


let result = diesel::select(json_array_length::<Json, _>(json!([1, 2, 3])))
    .get_result::<i32>(connection)?;
assert_eq!(result, 3);

let result = diesel::select(json_array_length::<Json, _>(json!([])))
    .get_result::<i32>(connection)?;
assert_eq!(result, 0);

let result = diesel::select(json_array_length::<Nullable<Json>, _>(None::<Value>))
    .get_result::<Option<i32>>(connection)?;
assert!(result.is_none());