Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Como corrigir:JSON_VALUE retorna NULL com strings longas (SQL Server)


Se você estiver usando JSON_VALUE() para retornar valores que consistem em uma string longa, você pode descobrir que ela retorna NULL em vez do valor real.

Ou você pode estar recebendo um erro.

A causa


Esse problema ocorre porque JSON_VALUE() retorna um valor de texto único do tipo nvarchar(4000) .

Ao usar JSON_VALUE() para retornar strings com mais de 4.000 caracteres, você receberá um erro ou um NULL valor, dependendo do modo de caminho que você está usando.

Modo Lax vs Modo Strict


Se você receber um erro ou NULL dependerá se você usa lax ou strict modo.

Quando o valor for maior que 4.000 caracteres:
  • Em lax modo, JSON_VALUE() retorna nulo.
  • Em strict modo, JSON_VALUE() retorna um erro.

Exemplo do problema


Aqui está um exemplo de código que causa o problema.

Vamos fazer dois exemplos; um em lax modo, e o outro em strict modo.

Modo relaxado

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');

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

Como mencionado, em lax modo ele retorna NULL .

Modo Estrito

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');

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

Como esperado, recebemos um erro. Felizmente, o erro fornece uma pista sobre o que deu errado, pois menciona que o valor da string seria truncado.

Solução


Felizmente, o OPENJSON() função não tem a mesma limitação de 4000 caracteres que JSON_VALUE() tem em seu valor de retorno.

OPENJSON() retorna valores JSON como um nvarchar(max) .

Portanto, podemos usar o código a seguir para resolver o problema.
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');

Resultado:
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.