Skip to main content

json_extract_json_2

Function json_extract_json_2 

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

Extracts a JSON object or array from a well-formed JSON document at the given path.

When a single path is provided, returns the value at that path as a Json value. When multiple paths are provided (using the variadic form), returns a JSON array

§Variadic functions

This function is variadic in SQL, so there’s a family of functions on a diesel side:

json_extract_json_0, json_extract_json_1, … json_extract_json_n

Here, the postfix number indicates repetitions of variadic arguments. To use this function, the appropriate version with the correct argument count must be selected.

§Controlling the generation of variadic function variants

By default, only variants with 0, 1, and 2 repetitions of variadic arguments are generated. To generate more variants, set the DIESEL_VARIADIC_FUNCTION_ARGS environment variable to the desired number of variants.

For a greater convenience this environment variable can also be set in a .cargo/config.toml file as described in the cargo documentation. containing the extracted values.

Returns NULL if a single path does not exist in the JSON document. With multiple paths, missing paths appear as null inside the returned JSON array.

To extract other types, use:

This function requires at least SQLite 3.9 or newer.

§Example


let json = json!({"a": 2, "c": [4, 5, {"f": 7}]});
let result = diesel::select(json_extract_json_1::<Json, _, _>(json, "$.c[2]"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!({"f": 7})), result);

let json = json!({"a": 2, "c": [4, 5], "f": 7});
let result = diesel::select(json_extract_json_2::<Json, _, _, _>(json, "$.c", "$.a"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!([[4, 5], 2])), result);

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

let json = json!({"a": 42});
let result = diesel::select(json_extract_json_2::<Json, _, _, _>(json, "$.a", "$.b"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!([42, null])), result);

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

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

let json = json!({"a": 42});
let result = diesel::select(json_extract_json_2::<Json, _, _, _>(json, "$.b", "$.c"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!([null, null])), result);