Following on from the blog post Convert XML to JSON Demo Video, we can also use Liquid Data Mapper to graphically create a data transform to run as a Microsoft Azure Function.
Updating the Transform for Azure
Using the same Data Mapper file as in the previous example, we now need to add an Input Parameter Component. This will act as an input to provide the Azure function with the XML data to translate. We can name this 'XMLData'.
We also need to add an Output Parameter Component. This tells the transform to output the JSON data as a string.
Next we will change the Debugger Configuration of the data transform to be of type 'Microsoft Azure Function Project':
Now when we click Debug->Compile (Ctrl+Shift+B), Liquid Data Mapper will generate a Microsoft Visual Studio project containing an Azure Function named 'ExampleFunction'. This provides a simple template function for reading parameterized data, transforming the data, and returning the result set within the Azure environment. If we compile and run the project, we can see the Azure Emulator runs and a console window opens telling us it is listening for connections:
ExampleFunction: [GET,POST] http://localhost:7071/api/ExampleFunction
Create a Test Application
In order to test the Azure data transform, we will create a simple C# console application in a separate instance of Visual Studio.
We will need to add a reference to the Nuget Package 'Newtonsoft.Json', and then add the following code:
static void Main(string[] args) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(
"http://localhost:7071/api/ExampleFunction"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var xmlData = File.ReadAllText(@"c:\example\BookStoreSample.xml"); var jrequest = new JObject(new JProperty("XmlData", xmlData),
new JProperty("outText", true)); streamWriter.Write(jrequest.ToString()); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); Console.Write(result); Console.ReadKey(); } }
By default, the Azure function expects the input parameters to be formatted as JSON. So we create a JObject and add a JProperty with the name "XmlData", and set its value to be our XML data which we are reading from the local file "BookStoreSample.xml".
We also add a second JProperty, "outText", instructing the function that we want the result sent back as plain text. If our function returned a sequence of values then we would need to wrap the result set in a container (e.g. outXml or outJson) in order to be able to distinguish one value from the next.
Testing the Azure Function
When we run the test application, it passes the XML input data to the Azure function and we can see it has been transformed into JSON output data, and the transform has also changed all of the 'title' values to be upper case:
Summary
Liquid Data Mapper is a flexible data transformation and mapping tool providing the power and flexibility to produce any combination of mapping, even combining data from multiple sources.
This simple example shows how you can quickly create a data transform for Microsoft Azure using an intuitive graphical interface.
Data Mapper Features
- Provides a graphical visualization of data mapping between multiple data sources and targets.
- Map data fields using an intuitive drag and drop UI.
- Support for CSV files, Databases, EDI data, JSON data, Text files, Web Services and XML data.
- Execute transform within Liquid Studio or generate mapping code for use in your own application.
- Step through debugger with breakpoints, stack trace, state and variable watch windows.
- Generate mapping code in XSLT or C#.
- Ideal for integrating with legacy systems.
Download a Free Trial of Liquid Studio and Liquid Data Mapper:
https://www.liquid-technologies.com/trial-download