Funzione get_json_object

Si applica a:segno di spunta sì Databricks SQL segno di spunta sì Databricks Runtime

Estrae un oggetto JSON da path.

Tip

Per il nuovo codice, Azure Databricks consiglia di usare il tipo di dati VARIANT con l'operatore : per eseguire query sui dati JSON. VARIANT offre prestazioni di lettura e scrittura migliori, accesso ai campi con distinzione tra maiuscole e minuscole e semantica più chiara rispetto all'analisi JSON basata su stringhe. Vedere Differenze tra varianti rispetto alle stringhe JSON.

Sintassi

get_json_object(expr, path)

Argomenti

  • expr: espressione STRING contenente JSON ben formato.
  • path: valore letterale STRING contenente un'espressione JSONPath rooted in $. Vedere Note per la sintassi supportata ed esempi per i modelli comuni.

Valori restituiti

UNA STRINGA.

Se l'oggetto non è stato trovato, viene restituito null.

Esempi

-- Extract a top-level field
> SELECT get_json_object('{"a":"b"}', '$.a');
 b

-- Extract a nested field
> SELECT get_json_object('{"a":{"b":"c"}}', '$.a.b');
 c

-- Extract an array element by index
> SELECT get_json_object('{"items":["apple","pear"]}', '$.items[0]');
 apple

-- Extract a field whose name contains a space
> SELECT get_json_object('{"first name":"Ada"}', '$["first name"]');
 Ada

-- Extract a field whose name contains a dot, using bracket notation with double quotes
> SELECT get_json_object('{"ids":{"a.b":"42"}}', '$.ids["a.b"]');
 42

-- Extract a field from every element of an array
> SELECT get_json_object('{"items":[{"n":1},{"n":2}]}', '$.items[*].n');
 [1,2]

-- Returns NULL when the path does not match
> SELECT get_json_object('{"a":"b"}', '$.missing');
 NULL

Note

get_json_object usa la sintassi JSONPath rooted in $. L'accesso al campo per notazione punto (.field) non fa distinzione tra maiuscole e minuscole. La notazione tra parentesi quadre (['field'] o ) fa distinzione tra maiuscole e ["field"]minuscole. Ciò differisce dall'espressione di percorso Azure Databricks JSON usata con l'operatore : per eseguire query sulle stringhe VARIANT e JSON, documentate separatamente.