简体   繁体   中英

SQL Server json truncated (even when using NVARCHAR(max) )

DECLARE @result NVARCHAR(max);

SET @result = (SELECT * FROM table
               FOR JSON AUTO, ROOT('Data'))

SELECT @result;

This returns a json string of ~43000 characters, with some results truncated.

SET @result = (SELECT * FROM table
               FOR JSON AUTO, ROOT('Data'))

This returns a json string of ~2000 characters. Is there any way to prevent any truncation? Even when dealing with some bigdata and the string is millions and millions of characters?

I didn't find and 'official' answer, but it seems that this is an error with the new 'FOR JSON' statement which is splitting the result in lines 2033 characters long. As recommendedhere the best option so far is to iterate through the results concatenating the returned rows:

string result = "";
while (reader.Read())
{
    result += Convert.ToString(reader[0]);                        
}

BTW, it seems that the latest versions of SSMS are already applying some kind of workaround like this to present the result in a single row.

I was able to get the full, non-truncated string by using print instead of select in SQL Server 2017 (version 14.0.2027):

DECLARE @result NVARCHAR(max);

SET @result = (SELECT * FROM table
           FOR JSON AUTO, ROOT('Data'))

PRINT @result;

Another option would be to download and useAzure Data Studio which I think is a multi-platform re-write of SSMS (similar to how Visual Studio was re-written as VS Code). It seems to spit out the entire, non-truncated json string as expected out of the box!

We've seen similar issues in SSMS, without using a variable SSMS truncates at 2033.

With a variable the query actually works OK when you use an nvarcahr(max) variable, but it truncates the output in the query results view at 43697.

A possible solution I've tested is outputting Query results to a file, using BCP:

bcp "DECLARE @result NVARCHAR(max); SET @result = (SELECT * FROM table FOR JSON AUTO, ROOT('Data')); SELECT @result as Result;" queryout "D:\\tmp\\exportOutput.txt" -S SQL_SERVER_NAME -T -w

See BCP docs for specifying server name\\instance and authentication options

I know this is an old thread but I have had success with this issue by sending the result to an XML variable. The advantage of using an XML variable is that the size is not stated as character length but by size of the string in memory, which can be changed in the options. Therefore Brad C's response would now look like...

DECLARE @result XML
SET @result = (SELECT * FROM table
FOR JSON AUTO, ROOT('Data'))
SELECT @result

or...

PRINT @result;

This will also work if you insert into a temp table - not presenting does not apply the truncate of SSMS. Might be usefull if you need to calculate several values.

declare @json table (j nvarchar(max));
insert into @json select * from(select* from Table where Criteria1 for json auto)a(j)
insert into @json select * from(select* from Table where Criteria2 for json auto)a(j)
select * from @json

It's difficult to determine exactly what the problem you're having without posting the data, but I had a similar problem when I was attempting to export a query in JSON format. The solution that worked for me was to go to Query/Query Options/Results/Text/Set "Maximum number of characters displayed in each column:" to 8192 (max value AFAIK).

This probably won't help much with your first query, but that potentially could be broken into smaller queries and executed successfully. I would anticipate that you could effectively run your second query after changing that setting.

Here is the answer to JSON truncation:

SQL divides the JSON result into chunks 2k in size (at least my SQL 2016 installation does), one chunk in the first column of each row in the result set. To get the entire result, your client code has to loop through the result set and concatenate the first column of each record. When you've gotten to the end of the rows, voila, your entire JSON result is retrieved, uncut.

When I first encountered the truncation problem I was baffled, and wrote off FOR JSON for several years as an unserious feature suited only to the smallest of datasets. I learned that I need to read the entire recordset only from the FOR XML documentation, and never actually saw it mentioned in the FOR JSON docs.

If your datalength is less than 65535 then you should use the suggestion of @dfundako who commented in the first post:

Try going to Tools, Options, Query Results, SQL Server, Results to Grid, and set Non-XML data to the max amount (I think 65535)

In my case the datalength was 21k characters so after exporting to grid I copied the value and it was fine, not truncated. Still it doesn't solve the the issue for those with bigger amount of data.

I'm using SQL Server 2019 and the issue is STILL THERE. Tried all replies on this post and none of them worked for me and it's not even a large dataset.

Try Visual Studio Code with Microsoft SQL extension. I got 6800 characters of JSON without truncation. It seems SSMS truncates results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM