Getting Started with Azure Functions : JSON to XML message Mapping | Azure Functions Using C# | Create Azure Functions Using C# example

{tocify} $title={Table of Contents}


Here let's see how to implement Azure function using Visual Studio with an example.

Scenario - Mapping single Json object into XML

You receive a purchase order in JSON format and it needs to be transformed into XML before sending it to downstream system.

Steps in  Solution

Here we will create http triggered based functions, which will accept JSON message and return XML transformed message.

1. Open Visual Studio, Click on File-->New-->Project and select Azure Functions as template

Create Function App Project

Click on Create and select the trigger, Storage emulator (it is local, when deploying to Azure, the function App has a storage account associated with it), Authorization Level 

select trigger for function

Click on create

2. This will result in creating a solution and a project inside it with couple of configuration files and .cs file with default code.

Create function using Template

It is the Azure function template which adds this files.

Function1.cs has a default function for accepting the http request, and checking for name in query parameter or in body.

host.json file contains configuration options that affect all functions in a function app instance( configurations like compiled app path). 

Local.settings.json file is actually container of application level configuration which is used while running functions locally.

It(configurations) has to be added in function Apps Application settings after function is deployed on Azure.

3. Add a new project to the solution to hold the class definition of the incoming message and outgoing message . (It can be done in same project also, but segregation based on purpose is always good)

project to hold models

Inorder.cs represents the incoming message

Incoming Message Model

OrderXml.cs represents the outgoing message

outgoing message model

4. In the first project delete the function1.cs file as we won't be using it rather we will create new SingleObjectMapping.cs in which we will add code to map JSON object to XML message 

First create a method named Run, but the method name can be any valid C# method name. Decorate it with Function Name [FunctionName("SingleObjectMapping")] - line 19 in below figure

The order of parameters in the function signature does not matter. For example, you can put the logger parameter before or after trigger or binding parameters and you can put trigger parameters before or after other bindings.

single object mapping

First we create an object of Outgoing message
OrderXml orderXml = new OrderXml(); -  line 26 in above figure

Read the http req body  as a  stream in a string variable requestBody and deserialize it as a JSON - line 28,29 in above figure

string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);ToEndAsync();

Now we have the incoming data deserialized in JSON, so the properties can be referenced and for mapping.

We create the orderXml object which we want to return -- line 30-33 in above figure
 orderXml.OrderId = data.OrderID;
 orderXml.CreatedDate = DateTime.Now.ToLongDateString();
 orderXml.OrderStatus = data.status;
 orderXml.Total = data.price;

Next we need to serialize above created object in XML, for that we make use of XmlSerializer and providing it typeof(OrderXml) to serialize as per OrderXml model -- line 35-38

 StringBuilder stringBuilder = new System.Text.StringBuilder();

 XmlSerializer serializer = new XmlSerializer(typeof(OrderXml));

 StringWriter sw = new StringWriter(stringBuilder);

 serializer.Serialize(sw, orderXml);

Last step, create response message and return it

string responseMessage = stringBuilder.ToString();

return new OkObjectResult(responseMessage);

That's it, our singleobjectmapping function is ready.

Now let's create another function, which will accept array of Json object and transform that into XML message.

Scenario - Mapping multiple  Json object into XML

Here the incoming request will be array of order in Json format which needs to be transformed into message having multiple orders but in XML format.

Steps in  Solution

1. Add support for holding multiple Json Object in Inorder.cs   (highlighted in yellow)
Inorder model for multiobject JSON

2. Add support for holding multiple xml in OrderXml.cs ( highlighted in yellow)

OrderXml model for multiple orders

3. Add new cs file with function for supporting the multiobject mapping

Multiobject mapping function

First, decorate the method with a functionname

In the method, create instance of Orders
Orders orders = new();

Read the stream of data received over http and deserialize the same against the Inorders (which we added in step 1), this will return Json array - store it in a variable.
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var jsons = JsonConvert.DeserializeObject<Inorders>(requestBody);

Here we need to create multiple OrderXml  objects, same as number of Json object in the array i.e.for each Json object in array OrderXml object will be created and add those in Orders object (see in model definition in step 2 where we have defined Orders as list of OrderXml.
orders.Orderxmls = new List<OrderXml>(); //Initializing Orders object 
      foreach (var json in jsons.inorders)
                OrderXml orderXml = new OrderXml(); //Initializing OrderXML object 

                orderXml.OrderId = json.orderId;
                orderXml.CreatedDate = json.createdDate;
                orderXml.OrderStatus = json.orderStatus;
                orderXml.Total =;

                orderXml.Address = new List<Address>(); //Initializing Address object 
                foreach (var addr in json.addr)
                    Address address = new Address();
                    address.Type = addr.Type;
                    address.City = addr.City;
                    address.State = addr.State;
                    address.PostalCode = addr.PostalCode;


                orders.Orderxmls.Add(orderXml); //Adding each orderXml in Orders


            StringBuilder stringBuilder = new System.Text.StringBuilder();
            XmlSerializer serializer = new XmlSerializer(typeof(Model.Orders));
            StringWriter sw = new StringWriter(stringBuilder);
            serializer.Serialize(sw, orders);

            string responseMessage = stringBuilder.ToString();

            return new OkObjectResult(responseMessage);

That's it, our second function - Multiobjectmapping  is also ready.



We can test this locally, to do that click on Run, it will build and following screen will be presented with the urls (would be localhost as we are testing locally, post deployment to Azure, url will be different).

build and run functions locally

Copy the urls and through any rest api clients like Postman/ARC test the functions.

SingleObjectMapping testing

singleObjectMapping function testing locally

MultiObjectMapping function testing

multiobjectmapping function testing locally

In the response note that we are getting multiple orderxml created.

Deployment to Azure

We have created two functions but it will be part of single function app.

Thus first we need to procure Function App and then deploy the functions in it.

Procure Azure Function App

Go to Azure portal and search for Function App and click on create

creating function app in Azure

Provide Name, Runtime stack , region etc.
Create function App - hosting options

Function app needs Storage account - you can use existing one or create new, operating system as Windows and Plan type as Consumption(Serverless). Click on Review+Create
create function app - review+create

You can go through other tabs also to modify the settings, once ok click on Create

MappingFunctionApp in azure

FunctionApp is procured now, check the url. 

Deploy/Publish Functions in above created Function App

Go to visual studio, open the solution and right click on MappinfunctionApp project and click on Publish.

Publish Azure Function from Visual Studio

From the list of Target select Azure and click Next

Select Azure function as specific Target

Select Azure function app(Windows) as Specific Target 
and click Next

Login screen for Function Instance

If you are not already signed in, then sign in . If you don't have azure account then create and login.

Select function App Instance

Select function App Instance under which you want to deploy functions and click Finish.

Ready to Publish

On Ready to Publish review the settings and click on Publish button right top

functions  published to  function App

Functions  published to  function App, go to portal and validate 

Functions deployed in function App

As can be seen, Functions are deployed in function App and are in Enabled state.

Note : When you deploy/publish from Visual studio, it is the .dll file which is deployed and thus you can't see the code in the  Portal.

Now, it's ready for use.. but let's test if first.

We have two options here to do testing
1. Testing in the portal itself (will test SingleobjectMapping function)
2. Copy the function url and test using Postman/ARC/Any rest api client
(will test multiobjectMapping function)

SingleObjectMapping testing

Test SingleObjectMappingFunction

Click on Test/Run , provide the input in Body section and click on Run
test singleobjectmapping function

Following is the output
Output of singleobject mapping function

MultiObjectMapping function testing


Click on Get Function URL and Copy it. Open any rest api client (I have used ARC)

Multiobjectmapping function testing using ARC


We saw a sample/example of how to create Azure functions in visual studio and how to test the azure functions  locally.

How to publish it to Azure and how to test it in Azure itself.

If you have questions or suggestions, feel free to do in comments section below !!!

Do share if you find this helpful .......
                          Knowledge Sharing is Caring !!!!!!


If you have any suggestions or questions or want to share something then please drop a comment

  1. Hi
    I have a Logic app which takes JSON as an input.

    I would like to trigger the logic app in a frequent interval of time with the same JSON input..
    How can I achieve this?

    Using Azure function we can trigger Logic app, but how to pass the input JSON message?

    If service bus trigger Azure Function, then how to keep sending the same message to Service message?

    Thanks in Advance

    1. Hi Bala,

      Like you yourself mentioned it, using Azure function you can call logic app.

      Create a Timer trigger Azure function and set the interval you want.

      Inside Azure function you can create variable which holds the static JSON message and use httpclient with post method with content (variable) to invoke the logic app.

      Last question, if I understand correct you need to have another process(may be a function) which pushes message to service bus or in the same function you can do it with a delay but it will go in loop.

      But not clear as to why you want to do it?

Previous Post Next Post