Funzione JSON_EXTRACT_PATH_TEXT - Amazon Redshift

Amazon Redshift non supporterà più la creazione di nuove UDF Python a partire dal 1º novembre 2025. Se desideri utilizzare le UDF Python, creale prima di tale data. Le UDF Python esistenti continueranno a funzionare normalmente. Per ulteriori informazioni, consulta il post del blog.

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, consigliamo di analizzare le stringhe JSON utilizzando Funzione JSON_PARSE per ottenere un valore SUPER. Quindi esegui query sull’elemento che desideri utilizzando la sintassi value.attribute. Per ulteriori informazioni sull’esecuzione di query sugli elementi dell’array nei valori SUPER, consulta Query sui 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 restituisce 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 ] )

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 BOOLEAN 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, specifica TRUE (t). Per restituire un errore se JSON non è valido, specificare FALSE (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 VARCHAR che 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 NULL per JSON non valido invece di restituire un errore, utilizza l’esempio seguente.

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

Considera l’esempio seguente, che seleziona 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 | +------------------------+

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 per il 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 |

Considera le seguenti istruzioni di esempio. L’argomento 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

Considera le seguenti istruzioni di esempio. Quando null_if_invalid è TRUE, JSON_EXTRACT_PATH_TEXT restituisce NULL quando json_string è JSON non 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

Considera gli esempi seguenti, dove 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 non esistente, 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