Skip to main content

jsonb_extract_jsonb_1

Function jsonb_extract_jsonb_1 

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

Extracts a JSON object or array from a well-formed JSON or JSONB document at the given path, returning it in JSONB format.

Unlike json_extract_json, which returns JSON objects and arrays

§Variadic functions

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

jsonb_extract_jsonb_0, jsonb_extract_jsonb_1, … jsonb_extract_jsonb_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. as text, this function returns them in the internal JSONB binary format. For scalar values (text, integer, double, null), both functions behave identically.

When a single path is provided, returns the value at that path as a Jsonb value. When multiple paths are provided (using the variadic form), returns a JSONB array 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 JSONB 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(jsonb_extract_jsonb_1::<Json, _, _>(json, "$"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!({"a": 2, "c": [4, 5, {"f": 7}]})), result);

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

let json = json!({"a": 2, "c": [4, 5, {"f": 7}]});
let result = diesel::select(jsonb_extract_jsonb_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]});
let result = diesel::select(jsonb_extract_jsonb_2::<Json, _, _, _>(json, "$.c", "$.x"))
    .get_result::<Option<Value>>(connection)?;
assert_eq!(Some(json!([[4, 5], null])), result);

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

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

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

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