Posted on

in

Simplifying XML-to-JSON Transformations in Logic Apps with JavaScript

In the realm of integration solutions, Azure Logic Apps stands out for its flexibility and power, enabling developers to design workflows that connect disparate systems with minimal code. One common challenge in such integrations is handling data transformations, particularly from XML to JSON format. While Logic Apps provides built-in support for such transformations, the resulting JSON often contains namespaces inherited from the XML structure, complicating further data manipulation. This blog post introduces a JavaScript solution designed to streamline this process by removing namespaces from JSON property names, following a transformation initiated by a “Compose” action. This approach ensures a cleaner, more manageable JSON structure for downstream processing.

Initial Transformation: The ConvertToJson Action

Before delving into the JavaScript solution, it’s important to understand the initial step in our workflow: the ConvertToJson Compose action. This action serves as the foundation for our namespace cleanup process, converting the XML message into JSON format using the json function provided by Logic Apps. The ConvertToJson action is critical, as it prepares the XML data for further processing, ensuring that the subsequent JavaScript action receives the data in JSON format.

The Challenge: Namespaces in JSON Transformed from XML

When converting XML data to JSON in Logic Apps, namespaces defined in the XML are retained in the JSON property names. This inclusion can lead to cumbersome property names, complicating data access and manipulation. For instance, property names might appear as {namespace}:PropertyName rather than the desired PropertyName. In the context of integration workflows, where simplicity and readability are paramount, such complexities can significantly hamper development efficiency.

The Solution: A JavaScript Cleanup Action

To address this challenge, we introduce a JavaScript action within the Logic Apps workflow, specifically designed to clean up namespace prefixes from JSON property names. This solution follows the conversion action, ensuring that the transformed JSON is immediately processed to remove unwanted namespaces, thereby simplifying the JSON structure for subsequent actions.

Retrieving the Transformed JSON

Our solution begins by retrieving the JSON object output from the previous ConvertToJson action. This step ensures we’re working with the most recent data representation:

var transformedJson = workflowContext.actions.ConvertToJson.outputs;

The cleanJsonNamespaces Function

At the core of our solution is the cleanJsonNamespaces function, designed to recursively traverse the JSON object, identifying and removing namespaces from property names. This function employs a two-fold approach:

  1. Detecting and Removing Namespaces: It first identifies properties with namespaces ({namespace}:PropertyName) and properties specific to XML metadata (?xml, @xmlns) that are irrelevant in the JSON context. These are either removed or cleaned to exclude the namespace prefix.
  2. Recursive Cleaning: Recognizing that JSON structures can be deeply nested with arrays and objects, the function recursively applies the cleaning process to ensure all levels of the structure are sanitized.

Implementation Details

Here’s a closer look at the cleanPropertyNames inner function, which is responsible for the recursive traversal and cleaning of the JSON object:

function cleanPropertyNames(object) {
    for (let propertyName in object) {
        // Conditions for deletion or cleaning of properties
        if (propertyName === '?xml' || propertyName.startsWith('@xmlns:')) {
            delete(object[propertyName]);
            continue;
        }
        let cleanedPropertyName = propertyName.replace(/.*:(.*)/, "$1");

        // Recursion for arrays and nested objects
        if (Array.isArray(object[propertyName])) {
            object[propertyName].forEach(element => {
                if (typeof element === 'object') {
                    cleanPropertyNames(element);
                }
            });
        } else if (typeof(object[propertyName]) === 'object') {
            cleanPropertyNames(object[propertyName]);
        }

        // Updating the object with cleaned property names
        if (propertyName !== cleanedPropertyName) {
            object[cleanedPropertyName] = object[propertyName];
            delete(object[propertyName]);
        }
    }    
}

By leveraging the JavaScript ability within Azure Logic Apps, this function meticulously ensures that all levels of the JSON structure are free from the complexities of namespaces, making the data more accessible and easier to work with.

Conclusion

Integrating systems often involves dealing with data in various formats, and transforming this data into a usable format is a critical step in any workflow. The JavaScript solution presented herein for Azure Logic Apps not only simplifies the transformation of XML to JSON by cleaning up namespaces but also exemplifies the power of custom code within a predominantly low-code environment. By applying such tailored solutions, developers can significantly enhance the efficiency and reliability of their integration workflows, focusing on the logic that drives business value rather than the intricacies of data formats.