

 Amazon Redshift non supporterà più la creazione di nuovi Python UDFs a partire dalla Patch 198. Python esistente UDFs continuerà a funzionare fino al 30 giugno 2026. Per ulteriori informazioni, consulta il [post del blog](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

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\$1EXTRACT\$1PATH\$1TEXT
<a name="JSON_EXTRACT_PATH_TEXT"></a>

**Nota**  
JSON\$1PARSE e le funzioni associate analizzano i valori JSON come SUPER, che Amazon Redshift analizza in modo più efficiente di VARCHAR.  
Invece di utilizzare JSON\$1EXTRACT\$1PATH\$1TEXT, consigliamo di analizzare le stringhe JSON utilizzando [Funzione JSON\$1PARSE](JSON_PARSE.md) 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](query-super.md).

La funzione JSON\$1EXTRACT\$1PATH\$1TEXT 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\$1EXTRACT\$1PATH\$1TEXT restituisce `NULL`.

Se l'argomento *null\$1if\$1invalid* è impostato su `TRUE` e la stringa JSON non è valida, la funzione restituisce `NULL` invece di restituire un errore.

JSON\$1EXTRACT\$1PATH\$1TEXT ha una dimensione massima di 64 KB. Pertanto, se un record JSON è più grande di 64 KB, l’elaborazione con JSON\$1EXTRACT\$1PATH\$1TEXT restituisce un errore. 

Per informazioni sulle funzioni JSON aggiuntive, consulta [Funzioni JSON](json-functions.md). Per ulteriori informazioni sull'utilizzo di JSON, consulta [COPY dal formato JSON](copy-usage_notes-copy-from-json.md).

## Sintassi
<a name="JSON_EXTRACT_PATH_TEXT-synopsis"></a>

```
JSON_EXTRACT_PATH_TEXT('json_string', 'path_elem' [,'path_elem'[, …] ] [, null_if_invalid ] )
```

## Arguments (Argomenti)
<a name="JSON_EXTRACT_PATH_TEXT-arguments"></a>

 *json\$1string*  
Una stringa JSON correttamente formattata.

*path\$1elem*  
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\$1if\$1invalid*  
(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](copy-usage_notes-copy-from-json.md#copy-usage-json-escape-characters).

## Tipo restituito
<a name="JSON_EXTRACT_PATH_TEXT-return"></a>

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

## Esempi
<a name="JSON_EXTRACT_PATH_TEXT-examples"></a>

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\$1if\$1invalid* 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\$1elem* fornito è NULL, quindi JSON\$1EXTRACT\$1PATH\$1TEXT 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\$1if\$1invalid* è TRUE, JSON\$1EXTRACT\$1PATH\$1TEXT restituisce NULL quando *json\$1string* è JSON non valido. Se *null\$1if\$1invalid* è FALSE o non è impostato, la funzione restituisce un errore quando *json\$1string* 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\$1string* è JSON valido e *path\$1elem* fa riferimento a un valore JSON `null`. In questo caso JSON\$1EXTRACT\$1PATH\$1TEXT restituisce NULL. Allo stesso modo, quando *path\$1elem* fa riferimento a un valore non esistente, JSON\$1EXTRACT\$1PATH\$1TEXT restituisce NULL, indipendentemente dal valore di *null\$1if\$1invalid*.

```
--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
```