Funzione try_variant_get
Si applica a: Databricks SQL
Databricks Runtime 15.3 e versioni successive
Estrae un valore di tipo type
da variantExpr
, specificato da path
o NULL
se non è possibile eseguire il cast al tipo di destinazione.
Sintassi
try_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 o non è possibile eseguirne il cast in type
, NULL
viene restituito .
Per generare un errore quando il cast non riesce, usare variant_get.
Esempi
-- Simple example
> SELECT try_variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.data[1].a', 'string')
hello
-- missing path
> SELECT try_variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.missing', 'int')
null
-- Invalid cast
> SELECT try_variant_get(parse_json('{"key": 123, "data": [4, {"a": "hello"}, "str"]}'), '$.key', 'boolean')
null
-- These are synonymous to:
> SELECT '{"key": 123, "data": [4, {"a": "hello"}, "str"]}':data[1].a ?::STRING;
hello
> SELECT '{"key": 123, "data": [4, {"a": "hello"}, "str"]}':missing ?::STRING;
null
> SELECT '{"key": 123, "data": [4, {"a": "hello"}, "str"]}':key ?::BOOLEAN;
null