Funzione JSON_EXTRACT_PATH_TEXT - AWS Clean Rooms

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Funzione JSON_EXTRACT_PATH_TEXT

JSON_EXTRACT_PATH_TEXT restituisce il valore per la coppia chiave:valore a cui fa riferimento una serie di elementi di percorso in una stringa JSON. Il percorso JSON può essere nidificato fino a cinque livelli di profondità. Gli elementi del percorso fanno distinzione tra maiuscole e minuscole. Se un elemento del percorso non esiste nella stringa JSON, JSON_EXTRACT_PATH_TEXT restituisce una stringa vuota. Se l'argomento null_if_invalid è impostato su true e la stringa JSON non è valida, la funzione restituisce NULL invece di restituire un errore.

Per informazioni sulle funzioni JSON aggiuntive, consulta Funzioni JSON.

Sintassi

json_extract_path_text('json_string', 'path_elem' [,'path_elem'[, …] ] [, null_if_invalid ] )

Argomenti

json_string

Una stringa JSON correttamente formattata.

path_elem

Un elemento di percorso in una stringa JSON. Un elemento di percorso è obbligatorio. È possibile specificare elementi aggiuntivi del percorso, fino a cinque livelli di profondità.

null_if_invalid

Un valore booleano che specifica se restituire NULL se la stringa JSON di input non è valida invece di restituire un errore. Per restituire NULL se JSON non è valido, specificare true (t). Per restituire un errore se JSON non è valido, specificare false (f). Il valore predefinito è false.

In una stringa JSON, AWS Clean Rooms riconosce \n come carattere nuova riga e \t come carattere di tabulazione. Per caricare una barra rovesciata, crea una sequenza di escape con una barra rovesciata (\\).

Tipo restituito

La stringa VARCHAR che rappresenta il valore JSON cui fanno riferimento gli elementi di percorso.

Esempio

L'esempio seguente restituisce il valore per il percorso 'f4', 'f6'.

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4', 'f6'); json_extract_path_text ---------------------- star

L'esempio seguente restituisce un errore perché JSON non è valido.

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6'); An error occurred when executing the SQL command: select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6')

L'esempio seguente imposta null_if_invalid su vero, in modo che l'istruzione restituisce NULL per JSON non valido invece di restituire un errore per JSON non valido.

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6',true); json_extract_path_text ------------------------------- NULL

L'esempio seguente restituisce il valore per il percorso.'farm', 'barn', 'color', dove il valore recuperato si trova al terzo livello. Questo esempio è formattato con uno strumento JSON Lint per semplificarne la lettura.

select json_extract_path_text('{ "farm": { "barn": { "color": "red", "feed stocked": true } } }', 'farm', 'barn', 'color'); json_extract_path_text ---------------------- red

L'esempio seguente restituisce NULL perché l'elemento 'color' risulta mancante. Questo esempio è formattato con uno strumento JSON Lint.

select json_extract_path_text('{ "farm": { "barn": {} } }', 'farm', 'barn', 'color'); json_extract_path_text ---------------------- NULL

Se il formato JSON è valido, il tentativo di estrarre un elemento mancante restituisce NULL.

L'esempio seguente restituisce il valore per il percorso 'house', 'appliances', 'washing machine', 'brand'.

select json_extract_path_text('{ "house": { "address": { "street": "123 Any St.", "city": "Any Town", "state": "FL", "zip": "32830" }, "bathroom": { "color": "green", "shower": true }, "appliances": { "washing machine": { "brand": "Any Brand", "color": "beige" }, "dryer": { "brand": "Any Brand", "color": "white" } } } }', 'house', 'appliances', 'washing machine', 'brand'); json_extract_path_text ---------------------- Any Brand