How to use dynamic JSON in ASP.NET Web API?

JObject and JArray class of JSON.NET can be used in Web API controller action method.

You can pass as method parameter or you can use as return type.

Please read my previous article, If you are new to dynamic JSON.

Dynamic JSON

 

Using Dynamic JSON in ASP.NET Web API:

Create a web API project using following steps:

Step1: Open Visual studio.

Step2: Goto File->New->Project->Web->ASP.NET Web Application-> Enter WebAPI project “DynamicJSONWebApi”.

image

Step3: Click OK->Web API –> Ok.

image

 

Step4: Click ValuesController from Controller folder and delete all other action method then Write a post method in ValuesControler as shown below.

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace DynamicJSONWebApi.Controllers
{
public class ValuesController : ApiController
{
[HttpPost]
public dynamic  GetDynamicJSONData(dynamic dynamicRequest)
{
dynamic newRequest = new JObject();
newRequest.FirstName = “Rajesh”;
newRequest.LastName = “Singh”;
newRequest.City = dynamicRequest.City;
newRequest.Country = dynamicRequest.Country;

            return newRequest;
}

    }
}

 

image

 

Dynamic object dynamicRequest is passed as input JSON parameter.

 

Request JSON:

{
“City”:”Bangalore”,
“Country”:”India”
}

 

I created a dynamic newRequest object of JObject and assign value of dynamic property FirstName , LastName, City and Country. City and Country are coming from input JSON.

 

Step4: Run WebAPI by pressing F5.

Step5: Open Google chrome. Click Apps from left side of chrome –> Click PostMan.

Step6: Select Request type POST and enter URL as shown below.

Post Webapi request to get dynamic JSON response

URL: http://localhost:54876/api/Values/GetDynamicJSONData/

Request JSON:

{
“City”:”Bangalore”,
“Country”:”India”
}

Click body and select JSON(application/json) as shown above image, then enter the request JSON.

Response JSON:

{
“FirstName”: “Rajesh”,
“LastName”: “Singh”,
“City”: “Bangalore”,
“Country”: “India”
}

 

You will get above response JSON.

 

Conclusion:

  • Dynamic input parameter can be passed to WebAPI method.
  • Return type of method also can be dynamic.
  • JObject and JArray of JSON.NET class are used to create dynamic JSON.

 

Please provide your valuable comments and suggestions.

Thanks for reading. Happy CodingSmile

3 Comments

  1. is it possible to make children under children like “{[[]]}”
    example:
    {
    “desc”: “Apps – Corporate”,
    “cod”: “APP_COR_ADM”,
    “open”: “false”,
    “children”: [
    {
    “desc”: “Activity Plan”,
    “cod”: “CORTRAPLA”,
    “open”: “false”,
    “children”: [
    {
    “desc”: “Activity Plan”,
    “cod”: “#”,
    “open”: “false”
    },

    {
    “desc”: “Feedback”,
    “cod”: “#”,
    “open”: “false”,
    “children”: [
    {
    “desc”: “Feedback – Insert”,
    “cod”: “#”,
    “open”: “false”
    },
    {
    “desc”: “Feedback – Search”,
    “cod”: “#”,
    “open”: “false”
    }

    ]
    }
    ]
    },
    {
    “desc”: “Budget & Forecast”,
    “cod”: “CORBDG”,
    “open”: “false”
    },
    {
    “desc”: “User Equipment”,
    “cod”: “USREQU”,
    “open”: “false”
    },
    {
    “desc”: “To-do Management”,
    “cod”: “PRJMGM”,
    “open”: “false”
    },
    {
    “desc”: “Mobile Devices”,
    “cod”: “MOBDEV”,
    “open”: “false”
    },
    {
    “desc”: “Purchase Request”,
    “cod”: “CORRDA”,
    “open”: “false”
    },
    {
    “desc”: “Skills & Expertness”,
    “cod”: “CORPOLI”,
    “open”: “false”
    },
    {
    “desc”: “Visitors Book”,
    “cod”: “CORVISITORS”,
    “open”: “false”
    },
    {
    “desc”: “Maintenance Devices”,
    “cod”: “CORMAIDEV”,
    “open”: “false”
    },
    {
    “desc”: “Projects”,
    “cod”: “CORPROJ”,
    “open”: “false”
    },
    {
    “desc”: “Product Maker”,
    “cod”: “CORPRDMKR”,
    “open”: “false”
    },
    {
    “desc”: “Quality Management”,
    “cod”: “CORQUAMNG”,
    “open”: “false”
    },
    {
    “desc”: “Product Hierarchy”,
    “cod”: “CORPRDHIER”,
    “open”: “false”
    },
    {
    “desc”: “Price Lists”,
    “cod”: “CORPRICELISTS”,
    “open”: “false”
    },
    {
    “desc”: “Meal Vouchers”,
    “cod”: “CORMEAL”,
    “open”: “false”
    },
    {
    “desc”: “Training Management”,
    “cod”: “TRAMNG”,
    “open”: “false”
    },
    {
    “desc”: “Manage Errors – Costs”,
    “cod”: “CHKCOSTI”,
    “open”: “false”
    },
    {
    “desc”: “Staff Training”,
    “cod”: “STATRG”,
    “open”: “false”
    }
    ]
    }
    please replay

      • [{“Price”:”LKR 37404″,”FlhtSegLst”:[{ “OperatedBy”:”SilkAir (Singapore) Pty Ltd”,”Airlinename”:”SriLankan Airlines”,”AirlineCode”:”UL”,”FlhtNo”:”UL 306″,”Departure”:”20-12-2019 01:10 AM”,”Arrival”:”20-12-2019 07:40 AM”,”Origin”:”CMB-Colombo”,”Destination”:”SIN-Singapore”,”Duration”:”04:00:00″,”Class”:”V-Economy”,”Aircraft”:”Airbus Industrie A330-300″,”Baggage”:null}]},{“Price”:”LKR 43002″,”FlhtSegLst”:[{“OperatedBy”:”SilkAir (Singapore) Pty Ltd”,”Airlinename”:”Singapore Airlines”,”AirlineCode”:”SQ”,”FlhtNo”:”SQ 469″,”Departure”:”20-12-2019 12:45 AM”,”Arrival”:”20-12-2019 07:30 AM”,”Origin”:”CMB-Colombo”,”Destination”:”SIN-Singapore”,”Duration”:”04:15:00″,”Class”:”W-Economy”,”Aircraft”:”Airbus Industries A350-900″,”Baggage”:null}]},{“Price”:””,”FlhtSegLst”:[{“OperatedBy”:”SilkAir (Singapore) Pty Ltd”,”Airlinename”:”SilkAir (Singapore) Pty Ltd”,”AirlineCode”:”MI”,”FlhtNo”:”MI 5883″,”Departure”:”20-12-2019 12:45 AM”,”Arrival”:”20-12-2019 07:30 AM”,”Origin”:”CMB-Colombo”,”Destination”:”SIN-Singapore”,”Duration”:”4:15″,”Class”:”J”,”Aircraft”:”AIRBUS INDUSTRIE A350-900″,”Baggage”:null},{“OperatedBy”:”SilkAir (Singapore) Pty Ltd”,”Airlinename”:”SriLankan Airlines”,”AirlineCode”:”UL”,”FlhtNo”:”UL 306″,”Departure”:”20-12-2019 01:10 AM”,”Arrival”:”20-12-2019 07:40 AM”,”Origin”:”CMB-Colombo”,”Destination”:”SIN-Singapore”,”Duration”:”4:00″,”Class”:”M”,”Aircraft”:”AIRBUS INDUSTRIE A330-300″,”Baggage”:null}]}]

        Could you please advice on how to retrieve this ?

Leave a Reply

Your email address will not be published.


*