How To Return JSON Nested/ Sub Object From Controller In MVC

Background

A few days back, I got a requirement to form a nested JSON object which will update the data in the HTML element through AJAX.

Working online, I could find many articles related to simple JSON format, whereas it was very hard for me to find an article which shows the simple example of nested JSON objects.

So, I thought of writing this small article and sharing the same with others.

Official definition of JSON

  • JSON (JavaScript Object Notation) is a lightweight data-interchange format.
  • It is easy for humans to read and write.
  • It is easy for machines to parse and generate.

JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JsonResult Type in MVC

JsonResult is one of the important and frequently used ActionResult Types in MVC. It has been mainly used for sending data in a JSON format from the Controller.

Simple Nested Json Format

  1. Var testjson = new {  
  2.     Student = new {  
  3.             name: “sathish”,  
  4.             hobby: ”cricket”  
  5.         },  
  6.         Employee = new {  
  7.             name: ”ganesh”,  
  8.             hobby: ”carrom”  
  9.         }  
  10.     Employer = new {  
  11.         name: ”suresh”,  
  12.         hobby: ”football”  
  13.     }  
  14. };  

In Controller

Use this overload of JSON Method (Object, JsonRequestBehavior)

It will create a JsonResult object that serializes the specified object to JavaScript Object Notation (JSON) format using the specified JSON request behavior.

  1. public class HomeController: Controller {  
  2.     public JsonResult test() {  
  3.         Object testjson = new {  
  4.             Student = new {  
  5.                     name: “sathish”,  
  6.                     hobby: ”cricket”  
  7.                 },  
  8.                 Employee = new {  
  9.                     name: ”ganesh”,  
  10.                     hobby: ”carrom”  
  11.                 },  
  12.                 Employer = new {  
  13.                     name: ”suresh”,  
  14.                     hobby: ”football”  
  15.                 }  
  16.         };  
  17.         return Json(testjson, JsonRequestBehavior.AllowGet);  
  18.     } //end of action method  
  19. //end of controller  

In client side

Call the Controller action method and for simple test purposes, get the value from the JSON and show it in the JavaScript alert like the below code.


  1. $.ajax({  
  2.     url: "../Home /test”,  
  3.     type: "get",  
  4.     dataType: "json",  
  5.     contentType: 'application/json; charset=utf-8',  
  6.     success: function(data) {  
  7.         alert(data.student.name)  
  8.         alert(data.Employee.name)  
  9.         alert(data.Employer.name)  
  10.     }  
  11.     error: function() {  
  12.         alert("in error function");  
  13.     }  
  14. }); //end of ajax event  

You can also find this or other related articles here.

Conclusion

I hope the above information was helpful. Let me know your thoughts and feedback.