Introduction
Pretty excited while writing this article as it’s one of the favorite topics for me. Before jumping into working with JSON, let’s first understand what it is.
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. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is a format to exchange & store data across platforms.
- JSON is the best alternative to XML
- JSON stores values in the key/value pair format.

How to work with JSON in ASP.NET?
- XML is expressed in more words than JSON. Hence, JSON is faster to write for programmers.
- JSON is lightweight and easy to write compared to XML.
- We can store values as key/value pair in JSON. Whereas, in XML, we have to store values between opening and closing tags.
Working with JSON in ASP.NET
- Using a free library, which would do all the complex work for you.
- Working with DataContracts that are built-in to the .NET framework.
- Writing your own “parser” for converting JSON into suitable format required.
In this article, I have used the free library named – JSON.NET. You can add this library to your project using the NuGet package manager. If you are not sure about adding the library, please refer to this article – Adding references using NuGet packages
Code Snippets
- public JObject ReadJSONData(string jsonFilename)
- {
- try
- {
- JObject jObject;
- // Read JSON directly from a file
- using (StreamReader file = System.IO.File.OpenText(jsonFilename))
- using (JsonTextReader reader = new JsonTextReader(file))
- {
- jObject = (JObject)JToken.ReadFrom(reader);
- }
- return jObject;
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Occurred : " + ex.Message);
- return null;
- }
- }
- //Read calendar info from JSON file
- JObject jObject = ReadJSONData(jsonFilename);
- for (int i = 0; i < jObject["employee"].Count(); i++)
- {
- //Your logic goes here...
- }
Example of JSON file
- {
- "employee": [{
- "name": "Shankar",
- "empid": "1",
- "department": "Design",
- }, {
- "name": "Ashish",
- "empid": "2",
- "department": "Testing",
- }, {
- "name": "Dipendra",
- "empid": "3",
- "department": "Development",
- }]
- }
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.
Read more articles on ASP.NET

tech knowledgePosted Jul 29, 2021, 9:21 AM
I want json format of mysql database table with the help of asp.net webapi
P KPosted Jul 11, 2021, 10:56 AM
Can u tell me where to write that code.............???????????/
Yogesh MhadgutPosted Dec 27, 2019, 5:36 AM
Please share complete artical on JSON with asp.net
Amit DavePosted May 3, 2016, 10:35 AM
Keep up the Good work!
Dipendra ShekhawatPosted May 2, 2016, 8:36 AM
Thanks Sonu Chaudhary !!
Sonu ChaudharyPosted May 2, 2016, 6:05 AM
Thanks for sharing
Dipendra ShekhawatPosted May 2, 2016, 1:45 AM
Thanks Guys !!!
NitinPosted May 1, 2016, 1:28 PM
Good one
Kuppurasu NagarajPosted Apr 30, 2016, 2:35 PM
Nice Sharing..