About
JsonResult is an ActionResult type in MVC. It helps to send the content in JavaScript Object Notation (JSON) format.
History
To learn more about ActionResult and some of the other following action result types, please go through the following articles in my blog.
- Action Result
- View Result
- Partial View Result with sample application
- Prevent PartialView to access directly from the URL
RoadMap
In this article, you will get an idea of the following things.
-
About JsonResult and its properties
- ContentEncoding
- ContentType
- Data
- JsonRequestBehavior
- MasJsonLength
- RecursionLimit
-
A sample project with various scenarios using JsonResult:
- Send JSON content welcome note based on user type
- Get the list of users in JSON Format
- How to create JSON data at the client side and send it to the controller
- How to handle a huge amount of JSON Data
-
Unit Testing of JsonResult
About JsonResult and its properties
The JSON format is an open standard format. The format of data looks very easy to understand and the data objects consist of attribute-value pairs.
ContentEncoding: It helps to indicate the content encoding type, the default encoding for JSON is UTF-8.
ContentType: It helps to indicate the content type. The default content type for JSON is application/json; charset=utf-8.
Note: ContentType and ContentEncoding are not necessary to mention when sending the data in JSON format as the HTTP headers are having a responsibility to tell the recipient what kind of content they're dealing with.
Data: This indicates what the content data is, that means what you will send in JSON format.
JsonRequestBehavior: This property has two options. Those are AllowGet and DenyGet. The default option is DenyGet. When you send data in JSON format, using Get Request, it's necessary to specify the property as AllowGet otherwise it shows the error as “The request would be blocked since the JSON data is considered as sensitive data information”.
MaxJsonLength: This helps to get or set the maximum JSON content length that you will send. The default value for this is 2097152 characters, that is equal to 4 MB of Unicode string data. You can even increase the size based if needed, for that you will get an idea later in this article.
RecursionLimit: Indicates the constraining number of object levels to process. The default value is 100. It means you can serialize the objects that are nested to a depth of 100 objects referencing each other. In a general scenario, the default limit 100 is obviously sufficient when you deal with a JsonResult so there is no need to increase it even though you have the option to increase the limit if required.
Sample Project with Various Scenarios by using JsonResult
Create a new project with the name JsonResultDemo and choose the template as MVC as shown in the following screenshots.

Now, click on the OK button then the displayed screen is as in the following.

As in the preceding template, you need to select the “Add Unit Tests” option as well. So It helps to create a Unit Test project and then again click on the OK button then the project will be created and the startup application page displayed like the following.

Now, add a controller and provide the name as “JsonDemoController” as in the following.

Click on the Controller and then it will open the popup window as in the following.

Now, click on the “Add” button and then it opens a popup to enter the name. So enter the name as “JsonDemoController” as shown in the screenshot.

After adding the controller to the project the controller page looks like the following.

Until now, you are done with the creation of the sample project template with the addition of one controller named “JsonDemoController”.
Scenario 1. Send JSON Content welcome note based on user type
In this scenario, you will learn how to send a simple welcome note message in JSON format from the controller. Now, replace the existing code with the following code in the JsonDemoController.cs file.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using JsonResultDemo.Models;
namespace JsonResultDemo.Controllers
{
public class JsonDemoController : Controller
{
#region ActionControllers
/// <summary>
/// Welcome Note Message
/// </summary>
/// <returns>In a Json Format</returns>
public JsonResult WelcomeNote()
{
bool isAdmin = false;
//TODO: Check the user if it is admin or normal user, (true-Admin, false- Normal user)
string output = isAdmin ? "Welcome to the Admin User" : "Welcome to the User";
return Json(output, JsonRequestBehavior.AllowGet);
}
}
}
Then, build the application (F6) and then hit the F5 to run an application and then navigate to the following URL http://localhost:49568/JsonDemo/WelcomeNote (It might be a chance to get a different Port Id at your end).
Then the displayed screen looks like the following.

In this scenario, you now have an idea of how to send a simple string in JSON format.
Scenario 2. Get the list of users in JSON Format
In this scenario, you will send a list of users in JSON format.
Step 1. Add a class file “UserModel.cs” like the following.

Click on “Class” and then the displayed link is as the following.

Enter the name as “UserModel.cs” and then click on the Add button.
Step 2. Update the code in UserMode.cs with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JsonResultDemo.Models
{
public class UserModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Company { get; set; }
}
}
Step 3. Add one method named GetUsers in the JsonDemoController.cs file that will return the list of sample users.
/// <summary>
/// Get the Users
/// </summary>
/// <returns></returns>
private List<UserModel> GetUsers()
{
var usersList = new List<UserModel>
{
new UserModel
{
UserId = 1,
UserName = "Ram",
Company = "Mindfire Solutions"
},
new UserModel
{
UserId = 1,
UserName = "chand",
Company = "Mindfire Solutions"
},
new UserModel
{
UserId = 1,
UserName = "Abc",
Company = "Abc Solutions"
}
};
return usersList;
}
Step 4. Create one Action Controller method named GetUsersData with the following code in the JsonDemoController.cs file.
/// <summary>
/// Get tthe Users data in Json Format
/// </summary>
/// <returns></returns>
public JsonResult GetUsersData()
{
var users = GetUsers();
return Json(users, JsonRequestBehavior.AllowGet);
}
Step 5. Run the application with this URL http://localhost:49568/JsonDemo/GetUsersData then the output looks like the following.

Scenario 3. Create JSON data at the client side and send content to the controller
In this scenario, you will create JSON data at the client side and then that data will be sent to the Controller action. The controller action request type is HttpPost.
Step 1. Create one Action controller method named Sample like the following in the JsonDemoController.cs file.
/// <summary>
/// Sample View
/// </summary>
/// <returns></returns>
public ActionResult Sample()
{
return View();
}
Step 2. Create a View file named “Sample.cshtml” by right-clicking on View() in the Sample action controller method then click on “Add View” in the Sample action like the following.

By clicking on Add View it opens a popup and deselects the "Use a layout page" option. It then should look as in the following.

Now, click on the OK button then the sample.cshtml file will be created.
Step 3. Replace it with the following cshtml code in the sample.cshtml file.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create Sample JSON Data and send it to controller</title>
</head>
<body>
<div>
<label>Create Sample User JSON Data and send it to controller</label><br/><br />
<input type="button" id="btnUpdateUserDetail" value="Update User Detail" onclick="UpdateUserDetail();"/>
</div>
</body>
</html>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script lang="en" type="text/javascript">
function UpdateUserDetail() {
var usersJson = GetSampleUsersList();
var getReportColumnsParams = {
"usersJson": usersJson
};
$.ajax({
type: "POST",
traditional: true,
async: false,
cache: false,
url: '/JsonDemo/UpdateUsersDetail',
context: document.body,
data: getReportColumnsParams,
success: function (result) {
alert(result);
},
error: function (xhr) {
//debugger;
console.log(xhr.responseText);
alert("Error has occurred..");
}
});
}
function GetSampleUsersList() {
var userDetails = {};
var usersList = [];
for (var i = 1; i <= 3; i++) {
userDetails["UserId"] = i;
userDetails["UserName"] = "User- " + i;
userDetails["Company"] = "Company- " + i;
usersList.push(userDetails);
}
return JSON.stringify(usersList);
}
</script>










Mohanasundaram NagarajPosted Apr 9, 2022, 6:17 AM
Thank You sir. Programming Simplified.......
sayyad hasanPosted May 15, 2021, 1:24 AM
Excellent article, explained in very simple way, thanks
Carlos Alberto Cassio VerdugoPosted Nov 30, 2019, 1:43 PM
Thank you Ramchand Repalle
Nga HanaPosted Nov 10, 2019, 10:15 PM
That it ok, but load data very slow. please help me load data fast
sibadutta NayakPosted Jun 6, 2019, 9:42 AM
It's very good article.
Faisal PathanPosted Aug 3, 2018, 6:56 AM
Override JsonResult : Its help me and save my time, Thanks
Sameer ParabPosted Apr 12, 2018, 4:46 AM
Awesome Article Bro !
Amatya AgyeyPosted Jan 17, 2018, 2:30 AM
Thats great, and much better.. Clear explanation dude :)
albert albertPosted Jul 11, 2017, 11:59 AM
HI, I only have one question. Why you did this: How can I put code in here?
Nithya MohanrajhPosted Jun 1, 2017, 1:00 AM
Very Clear explanation.Thanks for sharing.
Kalpesh SharmaPosted May 23, 2017, 2:18 AM
Thanks For the great Article.
Ashish AgarwalPosted Apr 26, 2017, 2:22 AM
Very nice Article..thanks for sharing. :)
Mohd. SayedPosted Apr 17, 2017, 5:44 AM
Thank you Ramchand Repalle
Avinash ThakurPosted Apr 13, 2017, 1:54 AM
Very Nice Article sir...
Ehsan SajjadPosted Mar 12, 2017, 9:15 AM
Very well written, good to start for beginners
Sivaraman DhamodaranPosted Jan 3, 2017, 1:51 AM
Nice Article. Thanks for sharing it.
Manav PandyaPosted Sep 25, 2016, 6:40 AM
Nice sir ...
sreenivasa kPosted Jul 25, 2016, 12:58 PM
Simply super
Munesh SharmaPosted Jun 20, 2016, 5:05 AM
nice
Thiruppathi RPosted Jun 10, 2016, 4:39 PM
Good article...
Atul RawatPosted Jun 3, 2016, 4:44 AM
nice article sir
Ramchand RepallePosted Mar 14, 2016, 7:30 AM
Thanks to all...
John CouturePosted Mar 2, 2016, 11:48 AM
This is an excellent article. Very well done. Thank you for sharing.
apex patelPosted Dec 4, 2015, 6:41 AM
Outstanding ..!!!...Excellent work !!..Thank you..
noor unnisaPosted Dec 1, 2015, 7:24 AM
awesome tutorial
Uday Bhan SinghPosted Nov 17, 2015, 4:49 AM
good job'
Sabyasachi MishraPosted Nov 11, 2015, 11:24 PM
Nice one and well explained.
Ramchand RepallePosted Oct 21, 2015, 2:58 AM
Thanks a lot guys..!!!
Nilesh JadavPosted Oct 21, 2015, 1:57 AM
Great Post !!
Debendra DashPosted Oct 21, 2015, 1:43 AM
good one..
Sibeesh VenuPosted Oct 21, 2015, 1:09 AM
Nice Share
Humayun Kabir MamunPosted Oct 21, 2015, 12:34 AM
Nice...
Mukesh KumarPosted Oct 21, 2015, 12:33 AM
Good Job
Harshad PansuriyaPosted Oct 21, 2015, 12:04 AM
Nice one
Mohammed IbrahimPosted Oct 20, 2015, 11:07 PM
nice
Ramchand RepallePosted Oct 16, 2015, 1:12 AM
Thanks to all... :)
Ajay KadamPosted Oct 15, 2015, 7:22 AM
great work helpful artical thanks..
Jyoti GuptaPosted Aug 3, 2015, 2:21 PM
Very Clear step by step clean code .Thank you for uploading nice article :)
sreenivasa kPosted Jul 15, 2015, 2:48 PM
nice
Ramchand RepallePosted Dec 14, 2014, 10:49 PM
ThanksVithal Wadje (y)
Vithal WadjePosted Dec 14, 2014, 10:44 PM
great work keep it up
Ramchand RepallePosted Dec 14, 2014, 10:41 PM
Thanks a lot to everyone... :)
Rahul Kumar SaxenaPosted Dec 13, 2014, 11:29 PM
Good Work... :)
Gaurav Kumar AroraPosted Dec 13, 2014, 3:54 AM
Great one!
Dinesh BeniwalPosted Dec 12, 2014, 10:30 PM
Great work Ramchand Repalle , one more article of the day on ASP.NET
Ch.Smrutiranjan ParidaPosted Nov 25, 2014, 5:45 AM
Good one.
chandra varmaPosted Nov 6, 2014, 1:18 PM
Excellent article sir
Nimit JoshiPosted Nov 5, 2014, 11:59 PM
Nice Article.
rahul rathorePosted Nov 4, 2014, 7:21 AM
well explained nice artcle .
Rasmita DashPosted Oct 25, 2014, 9:31 AM
Great one...
Damodara NaiduPosted Oct 21, 2014, 5:54 AM
Good article Ram. :)
Saineshwar BageriPosted Oct 17, 2014, 12:16 AM
nice article sir