How to create dynamic JSON?

What is JSON?

  • JSON is java script object notation.
  • JSON is lightweight format used to exchange data between client and server.
  • JSON is easy to read and write.
  • JSON is collection of name/value pair.
  • JSON is language independent.
  • JSON uses JavaScript syntax.
  • JSON format is text only.

Example:

var jsonObj = {“EmpId”:”1″, “EmpName”: “Rajesh”, “Salary”: “10000”};
var strJson = JSON.stringify(jsonObj);

JSON.stringify will convert json object to string.

 

What is Dynamic JSON?

JSON which can be created by object at runtime. Dynamic JSON can be created using JSON.NET.  JSON.NET is open source library to use JSON classes in .NET. JQuery and JArray classes are used to create dynamic JSON.

 

How to Use JSON.NET?

Create a console application in visual studio.

Step1: Open Visual studio.

Step2: Goto File->New->Project->Select Console application and enter project name.

Step3: Right click on Project Name then click  Manage Nuget Packages.

Step4: Type JSON.NET in search box right side, you will see JSON.NET as shown below.

Step5: Click install button in front of JSON.NET then it will install it.

Step6: Include these two name spaces.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 

image

 

Create Dynamic JSON:

I am using JObject and JArray class to create dynamic JSON as shown below.

 

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
   
    class Program
    {
        static void Main(string[] args)
        {
          
            dynamic employee = new JObject();
            employee.EmpId = 1;
            employee.Name = “Rajesh”;
            employee.Salary = 10000;
            employee.ContactNo = 9988223445;
            employee.City = “Bangalore”;
            employee.Address = new JArray(“Address1”, “Address2”);
            employee.Department = new JObject();
            employee.Department.DeptId = 1;
            employee.Department.DeptName= “R&D”;

            Console.WriteLine(employee.ToString());
          
            Console.ReadLine();
        }
    }
}

Output:

{
  “EmpId”: 1,
  “Name”: “Rajesh”,
  “Salary”: 10000,
  “ContactNo”: 9988223445,
  “City”: “Bangalore”,
  “Address”: [
    “Address1”,
    “Address2”
  ],
  “Department”: {
    “DeptId”: 1,
    “DeptName”: “R&D”
  }
}

Querying JSON:

Parse() method of JQuery and JArray is used to parse JSON string dynamically.

 

string json=@”{‘EmpId’: 1,
                  ‘Name’: ‘Rajesh’,
                  ‘Salary’: 10000,
                  ‘ContactNo’: 9988223445,
                  ‘City’: ‘Bangalore’,
                  ‘Address’: [
                    ‘Address1’,
                    ‘Address2’
                  ],
                  ‘Department’: {
                    ‘DeptId’: 1,
                    ‘DeptName’: ‘R&D’
                  }
                }
                “;

            dynamic jsonData = JObject.Parse(json);

            dynamic empid = jsonData.EmpId;
            dynamic name = jsonData.Name;
            dynamic salary = jsonData.Salary;
            dynamic contactNo = jsonData.ContactNo;
            dynamic city = jsonData.City;
            Console.WriteLine(“Empid: {0}, name:{1}, Salary:{2},ContactNo:{3},city:{4} “,empid,name,salary,contactNo,city);

   Here I have used same JSON string which we have created above.   

     

Output:

Empid: 1, name:Rajesh, Salary:10000,ContactNo:9988223445,city:Bangalore

Summary:

  • JSON is java script object notation.
  • JSON is lightweight format used to exchange data between client and server.
  • JSON is easy to read and write.
  • JSON is collection of name/value pair.
  • JSON is language independent.
  • JSON uses JavaScript syntax.
  • JSON format is text only.
  • JSON.NET is open source library to create dynamic JSON.
  • JObject and JArray classes are used to create dynamic JSON.
  • Parse() method of JObject and JArray is used to querying JSON with dynamic.

References:

https://www.newtonsoft.com/json/help/html/CreateJsonDynamic.htm

https://www.w3schools.com/js/js_json_intro.asp

https://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm

 

Please provide your comments and suggestions.

Thanks for reading. Happy coding.Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*