Skip to main content

json_type_with_path

Function json_type_with_path 

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

The json_type(X,P) function returns the “type” of the element in X that is selected by path P. If the path P in json_type(X,P) selects an element that does not exist in X, then this function returns NULL.

This function requires at least SQLite 3.38 or newer

§Example


let json_value = json!({"a": [2, 3.5, true, false, null, "x"]});

let result = diesel::select(json_type_with_path::<Json, _, _>(json_value.clone(), "$.a"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(Some("array".to_string()), result);

let result = diesel::select(json_type_with_path::<Json, _, _>(json_value.clone(), "$.a[0]"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(Some("integer".to_string()), result);

let result = diesel::select(json_type_with_path::<Json, _, _>(json_value.clone(), "$.a[1]"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(Some("real".to_string()), result);

let result = diesel::select(json_type_with_path::<Json, _, _>(json_value.clone(), "$.a[2]"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(Some("true".to_string()), result);

let result = diesel::select(json_type_with_path::<Json, _, _>(json_value.clone(), "$.a[6]"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(None, result);

let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json_value.clone(), "$.a"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(Some("array".to_string()), result);

let result = diesel::select(json_type_with_path::<Nullable<Json>, _, _>(None::<serde_json::Value>, "$.a"))
    .get_result::<Option<String>>(connection)?;

assert_eq!(None, result);