pub fn json_extract_json_1<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue, json, text_1>(
json: json,
text_1: text_1,
) -> json_extract_json_1<J, json, text_1>__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:
json_extract_stringfor text valuesjson_extract_integerfor integer valuesjson_extract_doublefor floating-point values
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);