Dear all below is my code although I have 2Lakh rows coming but the json size becomes 48MB , check the below code and let me know as when I run this code the visual studio get hangs
C#
public JsonResult GetEmployeesLocation(string Country)
LocationAddress loc=new LocationAddress();
try
{
using(DBENTITY db=new DBENTITY())
{
List MyLoc=(from m in db.locations where m.country=="USA"
select new LocationAddress
{
State=g.state,
City=g.city,
village=g.village,
state_ID=(int)g.SID,
City_ID=(int)C_ID,
Village_ID=(int)V_ID,
});
LocationAddress.location=MyLoc
return Json(LocationAddress,JsonRequestBehavior.AllowGet);
}
}when I hit the api using postman it says "error during serliazitaion or deserialization using hte json javascriptseriealizer, the length of the string exceeds the value set on maxjsonlength.
I have increased the size it return the data but system hangs as it is 48MB of data

Jignesh KumarRank #91 · 22.9K reputationPosted Sep 12, 2022, 11:40 PM EDT
Hi Mark,
Please use this to make it work,
public JsonResult GetEmployeesLocation(string Country) LocationAddress loc=new LocationAddress(); try { using(DBENTITY db=new DBENTITY()) { List MyLoc=(from m in db.locations where m.country=="USA" select new LocationAddress { State=g.state, City=g.city, village=g.village, state_ID=(int)g.SID, City_ID=(int)C_ID, Village_ID=(int)V_ID, }); LocationAddress.location=MyLoc return new JsonResult { Data = LocationAddress, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = int.MaxValue }; } }Mark TaborRank #914 · 2.3K reputationPosted Sep 13, 2022, 2:15 AM EDT
The issue is larger size around 48 MB Jignesh Kumar
Sachin SinghRank #66 · 26.2K reputationPosted Sep 13, 2022, 6:55 AM EDT
Which VS version are you using, for VS 2019 and VS 2022, this issue is fixed.
Please use the latest VS. You can still work on old workloads (webform) with VS 2022
Mark TaborRank #914 · 2.3K reputationPosted Sep 13, 2022, 10:03 AM EDT
Dear Sachin Singh its not related to vs i am checking using postman it is huge data even though if there are one million record it should not exceed 15-20 MB , the issue might be intentiating the new object everytime check the above code
Sachin SinghRank #66 · 26.2K reputationPosted Sep 13, 2022, 10:46 AM EDT
So, in VS you are getting data without any issue when you put debugger?
Mark TaborRank #914 · 2.3K reputationPosted Sep 13, 2022, 1:24 PM EDT
Thanks Sachin Singhv & Jignesh Kumar for the response , I am getting data in visual studio even in postman i am getting data as i increased the maxjonlength to int.maxvalue, but the issue is it return 48 MB of data which hands my machine and everything , actuall the table has 300000 rows and 6 columns only with int and string so how it can make 48 MB
Mark TaborRank #914 · 2.3K reputationPosted Sep 13, 2022, 2:21 PM EDT
"[\r\n {\r\n \"Result\": null,\r\n \"Description\": null,\r\n \"Id\": 0,\r\n \"employee\": \"fata\",\r\n \"department\":
getting this format how to format it
Anoop Kumar SharmaRank #127 · 17.4K reputationPosted Sep 13, 2022, 11:42 PM EDT
Hi Mark,
You can use the paging concept with the Web API to send the data in chunks based upon the page and page size. I already worked with the such APIs which returns large dataset.
You can also refer to below link in case you want to see pagination implementtion in ASP.NET Web API.
https://code-maze.com/paging-aspnet-core-webapi/
I hope this will help you.
Jignesh KumarRank #91 · 22.9K reputationPosted Sep 14, 2022, 12:37 AM EDT
Hi Mark,
If you have huge volume of data in respone then you can implement pagination and bring only require data to display.
https://www.c-sharpcorner.com/article/how-to-implement-paging-using-skip-and-take-operators-in-linq/
Mark TaborRank #914 · 2.3K reputationPosted Sep 14, 2022, 1:28 AM EDT
Jignesh Kumar dear I cannot do paging as It is not listing , The point is this is the manager login form when he login first time I want to get all location data from table having 200000 rows and save it in local storage , why the data is 60 MB , can we apply serialization or decerlizaation to reduce the size or my approach of api writing is wrong , I check your provided code as well but it still give lenth issue with return New JSON .
Vishal JoshiRank #377 · 6.7K reputationPosted Sep 16, 2022, 7:26 AM EDT
Hello Mark,
You can stringify JSON data to reduce size.
you can use library called Newtonsoft.Json. below is the common to install.
Install-Package Newtonsoft.Json -Version 13.0.1
Below is the sample code you can stringyfy JSON
return Json(JsonConvert.SerializeObject(LocationAddress),JsonRequestBehavior.AllowGet);It will convert data as below format
"[\r\n {\r\n \"Result\": null,\r\n \"Description\": null,\r\n \"Id\": 0,\r\n \"employee\": \"fata\",\r\n \"department\":
In Javascrip you can use JSON.Parse() to get back in the expected format.
Rijwan AnsariRank #82 · 24.2K reputationPosted Sep 17, 2022, 2:07 AM EDT
Check this
https://www.uptech.team/blog/filter-1gb-json-on-frontend-and-not-crash-browser
How To Filter 1GB JSON On The Frontend And Not Crash The Browser