Click here to Skip to main content
15,867,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Api with Response:

{
   "FullName":"Jon Black Kenedy",
   "EmployeeCode":"JBK0001",
   "Age":23,
   "CompanyCode":"RTA Shipter",
   "Orders":[
      {
         "ItemName":"meat",
         "Price":"$2000",
         "Quality":3,
         "Unit":"Peach"
      },
      {
         "ItemName":"Fruit",
         "Price":"$800",
         "Quality":60,
         "Unit":"Block"
      }
   ]
}


And I want to custom my response by middware (with custom configuration) to change my response to:
{
   "Customer_FullName":"Jon Black Kenedy",
   "Customer_Code":"JBK0001",
   "Request":[
      {
         "RequestItem":"meat",
         "Quality":3
      },
      {
         "RequestName":"Fruit",
         "Quality":60
      }
   ]
}


Content of Configuration File:

FullName-->Customer_FullName,
EmployeeCode-->Customer_Code,
Request[
	ItemName-->RequestItem,
	Quality-->Quality
	]


Please help me to resolve my problem. Thanks for all.

What I have tried:

I tried build my Custom Middware, but Api Response must be a class. What can i do now.
Posted
Updated 23-Jul-21 4:32am

1 solution

The way I'd do it is to create the appropriate class(es) (Class1) to read the JSON using (in my case) Newtonsoft, then create an output class (Class2) that matches what you want the new JSON to look like, with a assignment operator or constructor that excepts a Class1 instance.
Then it's trivial to generate the new JSON (agin, Newtonsoft for me).

That way, it's easy to accommodate changes in input or output without potentially large amounts of rework.
 
Share this answer
 
Comments
Mr.Duy09 26-Jul-21 4:24am    
Thanks bro. This is a good idea, I thought of this too, however it would have to use 2 fixed classes to be able to do it, but my real problem is the point that the properties are dynamic, so only applicable your solution when input and output are clear and fix

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900