json_array_length_with_path

Function json_array_length_with_path 

Source
pub fn json_array_length_with_path<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue, j, path>(
    j: j,
    path: path,
) -> json_array_length_with_path<J, j, path>
where j: AsExpression<J>, path: AsExpression<Text>,
Available on crate feature sqlite only.
Expand description

The json_array_length(X) function returns the number of elements in the JSON array X, or 0 if X is some kind of JSON value other than an array. The json_array_length(X,P) locates the array at path P within X and returns the length of that array, or 0 if path P locates an element in X that is not a JSON array, and NULL if path P does not locate any element of X. Errors are thrown if either X is not well-formed JSON or if P is not a well-formed path.

This function requires at least SQLite 3.46 or newer

ยงExample


let result = diesel::select(json_array_length_with_path::<Json, _, _>(json!([1,2,3,4]), "$"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(4), result);

let result = diesel::select(json_array_length_with_path::<Json, _, _>(json!([1,2,3,4]), "$[2]"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(0), result);

let result = diesel::select(json_array_length_with_path::<Json, _, _>(json!({"one":[1,2,3]}), "$.one"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(3), result);

let result = diesel::select(json_array_length_with_path::<Nullable<Json>, _, _>(json!({"one":[1,2,3]}), "$.two"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(None, result);

let result = diesel::select(json_array_length_with_path::<Jsonb, _, _>(json!([1,2,3,4]), "$"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(4), result);

let result = diesel::select(json_array_length_with_path::<Jsonb, _, _>(json!([1,2,3,4]), "$[2]"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(0), result);

let result = diesel::select(json_array_length_with_path::<Jsonb, _, _>(json!({"one":[1,2,3]}), "$.one"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(Some(3), result);

let result = diesel::select(json_array_length_with_path::<Nullable<Jsonb>, _, _>(json!({"one":[1,2,3]}), "$.two"))
    .get_result::<Option<i32>>(connection)?;

assert_eq!(None, result);