Amazon Redshift non supporterà più la creazione di nuovi Python a UDFs partire dal 1° novembre 2025. Se vuoi usare Python UDFs, crea la UDFs data precedente a quella data. Python esistente UDFs continuerà a funzionare normalmente. Per ulteriori informazioni, consulta il post del blog
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
Nota
JSON_PARSE e le funzioni associate analizzano i valori JSON come SUPER, che Amazon Redshift analizza in modo più efficiente di VARCHAR.
Invece di utilizzare JSON_EXTRACT_PATH_TEXT, ti consigliamo di analizzare le stringhe JSON utilizzando per ottenere un valore SUPER. Funzione JSON_PARSE Quindi, interroga l'elemento che desideri utilizzando la sintassi. value.attribute Per ulteriori informazioni sull'interrogazione degli elementi dell'array nei valori SUPER, vai a. Interrogazione di dati semistrutturati
La funzione 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 NULL.
Se l'argomento null_if_invalid è impostato su TRUE e la stringa JSON non è valida, la funzione restituisce NULL invece di restituire un errore.
JSON_EXTRACT_PATH_TEXT ha una dimensione massima di 64 KB. Pertanto, se un record JSON è più grande di 64 KB, l'elaborazione con JSON_EXTRACT_PATH_TEXT genera un errore.
Per informazioni sulle funzioni JSON aggiuntive, consulta Funzioni JSON. Per ulteriori informazioni sull'utilizzo di JSON, consulta COPY dal formato JSON.
Sintassi
JSON_EXTRACT_PATH_TEXT('json_string', 'path_elem' [,'path_elem'[, …] ] [, null_if_invalid ] )
Arguments (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
-
(Facoltativo) Un valore
BOOLEANche specifica se restituireNULLse la stringa JSON di input non è valida, invece di restituire un errore. Per restituireNULLse JSON non è valido, specificaTRUE(t). Per restituire un errore se JSON non è valido, specificareFALSE(f). Il valore predefinito èFALSE.
In una stringa JSON, Amazon Redshift riconosce \n come carattere newline e \t come carattere di tabulazione. Per caricare una barra rovesciata, crea una sequenza di escape con una barra rovesciata (\\). Per ulteriori informazioni, consultare Caratteri escape in JSON.
Tipo restituito
VARCHAR-
Una stringa
VARCHARche rappresenta il valore JSON cui fanno riferimento gli elementi di percorso.
Esempi
Per restituire il valore per il percorso 'f4', 'f6', utilizza l'esempio seguente.
SELECT JSON_EXTRACT_PATH_TEXT('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4', 'f6');+------------------------+ | json_extract_path_text | +------------------------+ | star | +------------------------+
Per restituire un errore poiché JSON non è valido, utilizza l'esempio seguente.
SELECT JSON_EXTRACT_PATH_TEXT('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6');ERROR: invalid json object {"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}
Per impostare null_if_invalid su TRUE, in modo che l'istruzione restituisca un codice JSON non valido anziché un errore, utilizzate l'esempio seguente. NULL
SELECT JSON_EXTRACT_PATH_TEXT('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6',true);+------------------------+ | json_extract_path_text | +------------------------+ | NULL | +------------------------+
Considerate l'esempio seguente, che seleziona il valore per il percorso, in cui il valore recuperato si trova al terzo livello'farm', 'barn', 'color', utilizzate l'esempio seguente. 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 | +------------------------+
Per restituire NULL perché l'elemento 'color' risulta mancante, utilizza l'esempio seguente. 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.
Per restituire il valore per il percorso 'house', 'appliances', 'washing machine', 'brand', utilizza l'esempio seguente.
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 | +------------------------+
L'esempio seguente crea una tabella di esempio e la popola con valori SUPER, quindi restituisce il valore del percorso 'f2' per entrambe le righe.
CREATE TABLE json_example(id INT, json_text SUPER); INSERT INTO json_example VALUES (1, JSON_PARSE('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}')), (2, JSON_PARSE('{ "farm": { "barn": { "color": "red", "feed stocked": true } } }')); SELECT * FROM json_example;id | json_text ------------+-------------------------------------------- 1 | {"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}} 2 | {"farm":{"barn":{"color":"red","feed stocked":true}}}SELECT id, JSON_EXTRACT_PATH_TEXT(JSON_SERIALIZE(json_text), 'f2') FROM json_example;id | json_text ------------+-------------------------------------------- 1 | {"f3":1} 2 |
Considerate le seguenti istruzioni di esempio. Il path_elem fornito è NULL, quindi JSON_EXTRACT_PATH_TEXT restituisce NULL indipendentemente dal valore di qualsiasi altro parametro.
--Statement where path_elem is NULL and json_string is valid JSON. SELECT JSON_EXTRACT_PATH_TEXT('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}',NULL);json_extract_path_text ------------------------ NULL--Statement where only one path_elem is NULL. SELECT JSON_EXTRACT_PATH_TEXT('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4',NULL);json_extract_path_text ------------------------ NULL--Statement where path_elem is NULL and json_string is invalid JSON. SELECT json_extract_path_text('invalid_json', NULL);json_extract_path_text ------------------------ NULL--Statement where path_elem is NULL and null_if_invalid is FALSE. SELECT json_extract_path_text(NULL, 0, FALSE);json_extract_path_text ------------------------ NULL
Considerate le seguenti istruzioni di esempio. Quando null_if_invalid è TRUE, JSON_EXTRACT_PATH_TEXT restituisce NULL quando json_string non è un JSON valido. Se null_if_invalid è FALSE o non è impostato, la funzione restituisce un errore quando json_string non è valido.
--Statement with invalid JSON where null_if_invalid is TRUE. SELECT json_extract_path_text('invalid_json', 0, TRUE);json_extract_path_text ------------------------ NULL--Statement with invalid JSON where null_if_invalid is FALSE. SELECT json_extract_path_text('invalid_json', 0, FALSE);ERROR: JSON parsing error
Considerate i seguenti esempi, in cui json_string è JSON valido e path_elem fa riferimento a un valore JSON. null In questo caso, JSON_EXTRACT_PATH_TEXT restituisce NULL. Allo stesso modo, quando path_elem fa riferimento a un valore inesistente, JSON_EXTRACT_PATH_TEXT restituisce NULL, indipendentemente dal valore di null_if_invalid.
--Statement selecting a null value. SELECT json_extract_path_text('[null]', 0); json_extract_path_text ------------------------- NULL --Statement selecting a non-existing value. SELECT json_extract_path_text('{}', 'a'); json_extract_path_text ------------------------- NULL