Condividi tramite


Funzione variant_get

Si applica a:segno di spunta sì Databricks SQL segno di spunta sì 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’espressione VARIANT.
  • path: valore STRING letterale con un'espressione di percorso JSON ben formata.
  • type: valore STRING 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.