Condividi tramite


JSON_PATH_EXISTS (Transact-SQL)

Si applica a: SQL Server 2022 (16.x) e versioni successive del databaseSQL di Azure Istanza gestita di SQL diAzure Azure Synapse Analyticsin Anteprima di Microsoft Fabric

La JSON_PATH_EXISTS sintassi verifica se esiste un percorso SQL/JSON specificato nella stringa JSON di input.

Convenzioni relative alla sintassi Transact-SQL

Syntax

JSON_PATH_EXISTS( value_expression , sql_json_path )

Arguments

value_expression

Espressione di carattere.

sql_json_path

Percorso SQL/JSON valido da testare nell'input.

Return value

Restituisce un valore int di 1 o 0 o NULL. Restituisce NULL se l'value_expression o l'input è un valore SQL NULL . Restituire 1 se il percorso SQL/JSON specificato esiste nell'input o restituisce una sequenza non vuota. In caso contrario restituisce 0.

La funzione JSON_PATH_EXISTS non restituisce errori.

Examples

Example 1

Nell'esempio seguente viene restituito 1 poiché la stringa JSON di input contiene il percorso SQL/JSON specificato. In questo esempio viene utilizzato un percorso annidato in cui la chiave è presente in un altro oggetto .

DECLARE @jsonInfo AS NVARCHAR (MAX);

SET @jsonInfo = N'{"info":{"address":[{"town":"Paris"},{"town":"London"}]}}';

SELECT JSON_PATH_EXISTS(@jsonInfo, '$.info.address');

Ecco il set di risultati.

1

Example 2

L'esempio seguente restituisce 0 poiché la stringa JSON di input non contiene il percorso SQL/JSON specificato.

DECLARE @jsonInfo AS NVARCHAR (MAX);

SET @jsonInfo = N'{"info":{"address":[{"town":"Paris"},{"town":"London"}]}}';

SELECT JSON_PATH_EXISTS(@jsonInfo, '$.info.addresses');

Ecco il set di risultati.

0

Example 3

Nell'esempio seguente viene JSON_PATH_EXISTS() usato con un carattere jolly:

DECLARE @jsonInfo AS NVARCHAR (MAX);

SET @jsonInfo = N'{"info":{"address":[{"town":"Paris"},{"town":"London"}]}}';

SELECT JSON_PATH_EXISTS(@jsonInfo, '$.info.address[*].town'); -- Returns: 1

Ecco il set di risultati.

1

Di seguito viene cercato almeno un elemento nella matrice con un oggetto con chiave towne ne trova uno.

SET @jsonInfo = N'{"info":{"address":[{"town":"Paris"},{"city":"London"}]}}';

SELECT JSON_PATH_EXISTS(@jsonInfo, '$.info.address[*].town'); -- Returns: 1  (at least one element in array has an object with key "town")

Ecco il set di risultati.

1

Di seguito viene cercato almeno un elemento nella matrice con un oggetto con chiave town, ma non trova nessuno.

SET @jsonInfo = N'{"info":{"address":[{"city":"Paris"},{"city":"London"}]}}';

SELECT JSON_PATH_EXISTS(@jsonInfo, '$.info.address[*].town'); -- Returns: 0 (no elements in array has an object with key "town")

Ecco il set di risultati.

0