sql >> Databáze >  >> RDS >> Sqlserver

Jak opravit:JSON_VALUE vrací NULL s dlouhými řetězci (SQL Server)

Pokud používáte JSON_VALUE() Chcete-li vrátit hodnoty skládající se z dlouhého řetězce, můžete zjistit, že vrací NULL místo skutečné hodnoty.

Nebo se vám může zobrazit chyba.

Příčina

K tomuto problému dochází, protože JSON_VALUE() vrátí jednu textovou hodnotu typu nvarchar(4000) .

Při použití JSON_VALUE() chcete-li vrátit řetězce delší než 4000 znaků, zobrazí se buď chyba, nebo NULL hodnotu v závislosti na režimu cesty, který používáte.

Laxní režim versus přísný režim

Ať už se zobrazí chyba nebo NULL bude záviset na tom, zda použijete lax nebo strict režimu.

Pokud je hodnota větší než 4000 znaků:

  • V lax režim, JSON_VALUE() vrátí hodnotu null.
  • V strict režim, JSON_VALUE() vrátí chybu.

Příklad problému

Zde je příklad kódu, který způsobuje problém.

Udělejme dva příklady; jeden v lax a druhý v strict režimu.

Laxní režim

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

Výsledek:

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

Jak již bylo zmíněno, v lax režimu vrací NULL .

Přísný režim

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

Výsledek:

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

Jak se očekávalo, dostaneme chybu. Naštěstí chyba poskytuje vodítko k tomu, co se pokazilo, protože zmiňuje, že hodnota řetězce bude zkrácena.

Řešení

Naštěstí OPENJSON() funkce nemá stejné omezení na 4000 znaků jako JSON_VALUE() má na své návratové hodnotě.

OPENJSON() vrátí hodnoty JSON jako nvarchar(max) .

Proto můžeme k vyřešení problému použít následující kód.

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

Výsledek:

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.

  1. 5 velmi častých chyb návrhu SQL dotazů, kterým se za každou cenu vyhnout

  2. PostgreSQL index na JSON

  3. Přeměňte výsledek databáze na pole

  4. C# volání oracle uložené funkce