Funzione variant_get
Si applica a: Databricks SQL Databricks Runtime 15.3 e versioni successive
Estrae un valore di tipo da variantExpr
, specificato da path
.
Sintassi
variant_get ( variantExpr, path, type )
Argomenti
-
variantExpr
: un’espressioneVARIANT
. -
path
: valoreSTRING
letterale con un'espressione di percorso JSON ben formata. -
type
: valoreSTRING
letterale che definisce il tipo.
Valori restituiti
Valore di tipo type
.
Se non è possibile trovare l'oggetto, NULL
viene restituito .
Se l'oggetto viene trovato ma il cast al tipo desiderato non può essere effettuato, Azure Databricks genera INVALID_VARIANT_CAST.
Per restituire NULL
invece di un errore, usare la funzione try_variant_get .
Esempi
-- Simple example
> SELECT variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.data[1].a', 'string')
hello
-- missing path
> SELECT variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.missing', 'int')
null
-- Invalid cast
> SELECT variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.key', 'array<int>')
Error: INVALID_VARIANT_CAST.