Skip to main content

jsonb_extract_string

Function jsonb_extract_string 

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

Extracts a text value from a well-formed JSON or JSONB document at the given path.

Works identically to json_extract_string for scalar text values.

Returns NULL if the path does not exist in the JSON document, or if the value at that path is a JSON null.

To extract other types, use:

This function requires at least SQLite 3.9 or newer.

ยงExample


let json = json!({"a": "xyz"});
let result = diesel::select(jsonb_extract_string::<Json, _, _>(json, "$.a"))
    .get_result::<Option<String>>(connection)?;
assert_eq!(Some("xyz".to_string()), result);

let json = json!({"a": 42});
let result = diesel::select(jsonb_extract_string::<Json, _, _>(json, "$.a"))
    .get_result::<Option<String>>(connection)?;
assert_eq!(Some("42".to_string()), result);

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

let json = json!({"a": "xyz"});
let result = diesel::select(jsonb_extract_string::<Json, _, _>(json, "$.x"))
    .get_result::<Option<String>>(connection)?;
assert!(result.is_none());