Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Comment réparer :JSON_VALUE renvoie NULL avec de longues chaînes (SQL Server)

Si vous utilisez JSON_VALUE() pour renvoyer des valeurs constituées d'une longue chaîne, vous pouvez constater qu'il renvoie NULL au lieu de la valeur réelle.

Ou, vous pourriez avoir une erreur.

La raison

Ce problème se produit car JSON_VALUE() renvoie une seule valeur de texte de type nvarchar(4000) .

Lors de l'utilisation de JSON_VALUE() pour renvoyer des chaînes de plus de 4 000 caractères, vous obtiendrez soit une erreur, soit un NULL valeur, selon le mode de chemin que vous utilisez.

Mode laxiste vs mode strict

Si vous obtenez une erreur ou NULL dépendra si vous utilisez lax ou strict mode.

Lorsque la valeur est supérieure à 4 000 caractères :

  • En lax mode, JSON_VALUE() renvoie null.
  • En strict mode, JSON_VALUE() renvoie une erreur.

Exemple du problème

Voici un exemple de code qui cause le problème.

Faisons deux exemples; un en lax mode, et l'autre en strict mode.

Mode laxiste

DECLARE @json nvarchar(max) = N'{ 
    "article" : {
            "id" : 1,
            "text" : "If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error."
    }
}';
SELECT JSON_VALUE(@json, 'lax $.article.text');

Résultat :

+--------------------+
| (No column name)   |
|--------------------|
| NULL               |
+--------------------+

Comme mentionné, en lax mode il renvoie NULL .

Mode strict

DECLARE @json nvarchar(max) = N'{ 
    "article" : {
            "id" : 1,
            "text" : "If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error."
    }
}';
SELECT JSON_VALUE(@json, 'strict $.article.text');

Résultat :

Msg 13625, Level 16, State 1, Line 7
String value in the specified JSON path would be truncated.

Comme prévu, nous obtenons une erreur. Heureusement, l'erreur fournit un indice sur ce qui s'est mal passé, car elle mentionne que la valeur de la chaîne serait tronquée.

Solution

Heureusement, le OPENJSON() la fonction n'a pas la même limite de 4000 caractères que JSON_VALUE() a sur sa valeur de retour.

OPENJSON() renvoie les valeurs JSON sous la forme d'un nvarchar(max) .

Par conséquent, nous pouvons utiliser le code suivant pour résoudre le problème.

DECLARE @json nvarchar(max) = N'{ 
    "article" : {
            "id" : 1,
            "text" : "If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error."
    }
}';
SELECT text FROM OPENJSON(@json, '$.article') 
WITH (text nvarchar(max) '$.text');

Résultat :

If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error. If you have to return scalar values greater than 4000 characters, use the OPENJSON function instead of JSON_VALUE. This text contains more than 4000 characters, which will cause issues for the JSON_VALUE function in SQL Server. This is because the JSON_VALUE function returns a single text value of type nvarchar(4000). If the value is greater than 4000 characters: In lax mode, JSON_VALUE returns null. In strict mode, JSON_VALUE returns an error.