Função try_variant_get
Aplica-se a: Databricks SQL Databricks Runtime 15.3 e posterior
Extrai um valor de tipo type
de , especificado por path
, ou NULL
se não for possível converter para o tipo de variantExpr
destino.
Sintaxe
try_variant_get ( variantExpr, path, type )
Argumentos
variantExpr
: UmaVARIANT
expressão.path
: UmSTRING
literal com uma expressão de caminho JSON bem formada.type
: UmSTRING
literal definindo o tipo.
Devoluções
Um valor do tipo type
.
Se o objeto não puder ser encontrado ou não puder ser convertido em type
, NULL
será retornado.
Para gerar um erro quando o elenco falhar, use variant_get.
Exemplos
-- 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