diesel::pg::expression::dsl

Function jsonb_array_length

source
pub fn jsonb_array_length<E: JsonbOrNullableJsonb + MaybeNullableValue<Integer>, jsonb>(
    jsonb: jsonb,
) -> jsonb_array_length<E, jsonb>
where jsonb: 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(jsonb_array_length::<Jsonb, _>(json!([1, 2, 3])))
    .get_result::<i32>(connection)?;
assert_eq!(result, 3);

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

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