Partager via


Fonction try_variant_get

S’applique à : coche pour oui Databricks SQL coche marquée oui Databricks Runtime 15.3 et versions ultérieures

Extrait une valeur de type type à partir de variantExpr, spécifiée par path, ou NULL s’il n’est pas possible d’effectuer un cast en type cible.

Syntaxe

try_variant_get ( variantExpr, path, type )

Arguments

  • variantExpr : expression VARIANT.
  • path : un littéral STRING avec une expression de chemin JSON bien formée.
  • type : un littéral STRING définissant le type.

Retours

Valeur de type type.

Si l’objet est introuvable ou ne peut pas faire l’objet d’un cast en type, la valeur NULL est retournée. Pour déclencher une erreur en cas d’échec du cast, utilisez variant_get.

Exemples

-- 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', 'array<int>')
  null