Search code examples
azureazure-logic-appsazureservicebus

Send XML in message service bus and parse it in logic app


As I am a beginner on azue, I would like to know the procedure to follow for the following process: sending an xml with a service bus message and receiving and parse it with logic app. Thanks


Solution

  • I have used 2 logic apps in this case where 1 sends an XML message through the service bus and the other receives it.

    The flow of Logic App - 1 (Sending XML message to Service bus)

    enter image description here

    The flow of Logic App - 2 (Receiving XML message from Service bus and parsing it)

    You can convert XML to JSON and then use Parse_JSON to parse it.

    enter image description here

    RESULT:

    enter image description here

    Codeview of Logic app - 1

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Send_message": {
                    "inputs": {
                        "body": {
                            "ContentData": "@{base64(outputs('XML_Content'))}",
                            "SessionId": "@{utcNow()}"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['servicebus']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/@{encodeURIComponent(encodeURIComponent('queue1'))}/messages"
                    },
                    "runAfter": {
                        "XML_Content": [
                            "Succeeded"
                        ]
                    },
                    "type": "ApiConnection"
                },
                "XML_Content": {
                    "inputs": "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>",
                    "runAfter": {},
                    "type": "Compose"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "servicebus": {
                        "connectionId": "/subscriptions/<SubId>/resourceGroups/<RG>/providers/Microsoft.Web/connections/servicebus-1",
                        "connectionName": "servicebus-1",
                        "id": "/subscriptions/<SubId>/providers/Microsoft.Web/locations/centralus/managedApis/servicebus"
                    }
                }
            }
        }
    }
    

    Codeview of Logic app - 2

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Compose": {
                    "inputs": "@json(xml(base64ToString(triggerBody()?['ContentData'])))",
                    "runAfter": {},
                    "type": "Compose"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": "@outputs('Compose')",
                        "schema": {
                            "properties": {
                                "note": {
                                    "properties": {
                                        "body": {
                                            "type": "string"
                                        },
                                        "from": {
                                            "type": "string"
                                        },
                                        "heading": {
                                            "type": "string"
                                        },
                                        "to": {
                                            "type": "string"
                                        }
                                    },
                                    "type": "object"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {
                        "Compose": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_a_message_is_received_in_a_queue_(auto-complete)": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['servicebus_1']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/@{encodeURIComponent(encodeURIComponent('queue1'))}/messages/head",
                        "queries": {
                            "queueType": "Main"
                        }
                    },
                    "recurrence": {
                        "frequency": "Second",
                        "interval": 1
                    },
                    "type": "ApiConnection"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "servicebus_1": {
                        "connectionId": "/subscriptions/<SubId>/resourceGroups/<RG>/providers/Microsoft.Web/connections/servicebus-2",
                        "connectionName": "servicebus-2",
                        "id": "/subscriptions/<SubId>/providers/Microsoft.Web/locations/centralus/managedApis/servicebus"
                    }
                }
            }
        }
    }