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

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

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)
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.
RESULT:
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"
}
}
}
}
}

Related

How to get a logic app to stop running after a successful run each day

I have a logic app that I want to run once a line item appears in a table each day. once that line item appear, i don't want the logic app to run anymore that day. Can't quite think of how to accomplish this.
Apart from using the related triggers you can use Terminate action to stop the flow.
After using Get entities (V2) I'm trying to Parse the result to get the timestamp of that particular entity to check if the table has an added row or not for that day
After parsing below is the condition that I'm using
{
"contains": [
"#formatDateTime(body('Parse_JSON')?[0]?['Timestamp'],'dd-MM-yyyy')",
"#formatDateTime(utcNow(),'dd-MM-yyyy')"]
}
Below is the complete flow of my Logic App
RESULTS:
To reproduce the same in your Logic App you can use the below code view
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Condition": {
"actions": {
"Terminate": {
"inputs": {
"runStatus": "Succeeded"
},
"runAfter": {},
"type": "Terminate"
}
},
"else": {
"actions": {
"Delay": {
"inputs": {
"interval": {
"count": 2,
"unit": "Hour"
}
},
"runAfter": {},
"type": "wait"
},
"HTTP": {
"inputs": {
"method": "POST",
"uri": "<URI>"
},
"runAfter": {
"Delay": [
"Succeeded"
]
},
"type": "Http"
}
}
},
"expression": {
"and": [
{
"contains": [
"#formatDateTime(body('Parse_JSON')?[0]?['Timestamp'],'dd-MM-yyyy')",
"#formatDateTime(utcNow(),'dd-MM-yyyy')"
]
}
]
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "If"
},
"Get_entities_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azuretables']['connectionId']"
}
},
"method": "get",
"path": "/v2/storageAccounts/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/tables/#{encodeURIComponent('<TableName>')}/entities"
},
"runAfter": {},
"type": "ApiConnection"
},
"Parse_JSON": {
"inputs": {
"content": "#body('Get_entities_(V2)')?['value']",
"schema": {
"items": {
"properties": {
"FirstName": {
"type": "string"
},
"LastName": {
"type": "string"
},
"PartitionKey": {
"type": "string"
},
"RowKey": {
"type": "string"
},
"Timestamp": {
"type": "string"
},
"odata.etag": {
"type": "string"
}
},
"required": [
"odata.etag",
"PartitionKey",
"RowKey",
"Timestamp",
"FirstName",
"LastName"
],
"type": "object"
},
"type": "array"
}
},
"runAfter": {
"Get_entities_(V2)": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azuretables": {
"connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/azuretables",
"connectionName": "azuretables",
"id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/azuretables"
}
}
}
}
}
Alternatively, you can use Recurrence trigger to trigger it in regular intervals with the same logic.

How do I parse Json file from azure blob storage to Azure Sql using Azure logic app

[][1]I have a multiple json files dropping in blob storage weekly, I want to use azure logic app to parse json file and copy data into Azure Sql? Please help
For achieving your requirement, Below are the flow that you can follow :-
Blob trigger (When a blob is added or modified (properties only) (V2)) >> Get blob content using path (V2) >> Parse JSON >> SQL related Action (For instance I'm using Insert row (V2)).
Below is the sample JSON that I'm uploading to my container.
{
"employees": {
"emp_name": "abc",
"hire_date": "2022-10-23",
"salary": 10000
}
}
I'm using triggers path to get the content of the blob. While Inserting the row I'm using Parse JSON values. Below is my Logic App.
Result:
UPDATED ANSWER
As per your requirement, you can either manually trigger flow using `` or set a recurrence trigger to make the flow triggered at times and then use list all files in that particular container from the storage account. Here is how the flow looks like
detailed flow
RESULTS:
code view of my logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Get_blob_content_using_path_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/GetFileContentByPath",
"queries": {
"inferContentType": true,
"path": "#items('For_each')?['Path']",
"queryParametersSingleEncoded": true
}
},
"runAfter": {},
"type": "ApiConnection"
},
"Insert_row_(V2)": {
"inputs": {
"body": {
"emp_id": "#body('Parse_JSON')?['employees']?['employee_id']",
"emp_name": "#body('Parse_JSON')?['employees']?['emp_name']",
"hire_date": "#body('Parse_JSON')?['employees']?['hire_date']",
"salary": "#body('Parse_JSON')?['employees']?['salary']"
},
"host": {
"connection": {
"name": "#parameters('$connections')['sql']['connectionId']"
}
},
"method": "post",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('default'))},#{encodeURIComponent(encodeURIComponent('default'))}/tables/#{encodeURIComponent(encodeURIComponent('[dbo].[employees]'))}/items"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Parse_JSON": {
"inputs": {
"content": "#json(body('Get_blob_content_using_path_(V2)'))",
"schema": {
"properties": {
"employees": {
"properties": {
"emp_name": {
"type": "string"
},
"employee_id": {
"type": "integer"
},
"hire_date": {
"type": "string"
},
"salary": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
},
"runAfter": {
"Get_blob_content_using_path_(V2)": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyMQ=='))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"metadata": {
"JTJmY29udGFpbmVyMQ==": "/container1"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<SUBSCRIPTION ID>/providers/Microsoft.Web/locations/centralus/managedApis/azureblob"
},
"sql": {
"connectionId": "/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/connections/sql",
"connectionName": "sql",
"id": "/subscriptions/<SUBSCRIPTION ID>/providers/Microsoft.Web/locations/centralus/managedApis/sql"
}
}
}
}
}

Azure Logic App HTTP Action step - dynamically get endOfDate date

i have an azure logic app with recurrence that will call api endpoint every midnight, im passing in two date properties in my request body that will include start of day and end of day.
Logic app has expression startOfDay() however they dont have endOfDay(), how can I dynamically get end of day in UTC format like startOfDay() does?
Thanks
this is how my request body looks like but its also complaining about #startOfDay()
{
"organizationId": 'f41186b0-7f09-42c5-8a9d-81a2ad2b0e61',
"attemptedDeliveries": true,
"cancelDateStart": #{startOfDay()},
"cancelDateEnd": ""
}
There is no direct expression for endOfDay but one of the workarounds is to addToTime and 'subtractFromTime' from startOfDay to get the endOfDay. Consider I'm taking the timestamp to be UtcNow() and to calculate the endOfDay I'm using the below expression.
subtractFromTime(addToTime(outputs('startOfDay'),1,'Day','o'),1,'Second','yyyy-MM-ddTHH:mm:ss')
I'm using Parse Json in order to retrieve the inner Json details for future use. By doing this, you can have a custom JSON created using Compose() connector.
Result:-
Codeview
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"attemptedDeliveries": "#body('Parse_JSON')?['attemptedDeliveries']",
"cancelDateEnd": "#{outputs('endOfDay')}",
"cancelDateStart": "#{outputs('startOfDay')}",
"organizationId": "#{body('Parse_JSON')?['organizationId']}"
},
"runAfter": {
"endOfDay": [
"Succeeded"
]
},
"type": "Compose"
},
"Parse_JSON": {
"inputs": {
"content": "#triggerBody()",
"schema": {
"properties": {
"attemptedDeliveries": {
"type": "boolean"
},
"cancelDateEnd": {
"type": "string"
},
"cancelDateStart": {
"type": "string"
},
"organizationId": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {},
"type": "ParseJson"
},
"endOfDay": {
"inputs": "#subtractFromTime(addToTime(startOfDay(utcNow()),1,'Day','o'),1,'Second','yyyy-MM-ddTHH:mm:ss')",
"runAfter": {
"startOfDay": [
"Succeeded"
]
},
"type": "Compose"
},
"startOfDay": {
"inputs": "#startOfDay(utcNow(),'yyyy-MM-ddTHH:mm:ss')",
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
Here is the json that I'm receiving from my body of Http trigger
{
"organizationId": "f41186b0-7f09-42c5-8a9d-81a2ad2b0e61",
"attemptedDeliveries": true,
"cancelDateStart": "",
"cancelDateEnd": ""
}
Updated Answer
After the follow up here my logic app
Result:
Codeview
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"endOfDay": {
"inputs": "#subtractFromTime(addToTime(startOfDay(utcNow()),1,'Day','o'),1,'Second','yyyy-MM-ddTHH:mm:ss')",
"runAfter": {
"startOfDay": [
"Succeeded"
]
},
"type": "Compose"
},
"startOfDay": {
"inputs": "#startOfDay(utcNow(),'yyyy-MM-ddTHH:mm:ss')",
"runAfter": {},
"type": "Compose"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Second",
"interval": 30
},
"recurrence": {
"frequency": "Second",
"interval": 30
},
"type": "Recurrence"
}
}
},
"parameters": {}
}
After endOfDay connector you can add 4 parallel HTTP triggers and add logic to it.
REFERENCES: Reference guide to workflow expression functions in Azure Logic Apps and Power Automate

Getting Error while trying to initiate Logic App Trigger

I am trying to configure a Logic App using Event Grid Trigger. The Trigger should be when my Azure CMK in my key vault is nearing expiry it should send me an email. I have configured the Logic App using the Logic App Designer, but when i try to run the trigger, it throws me the error as shown in the screenshot.
The screenshot for my designer is also attached. Any idea what do i need to do to fix this[![enter image description here][3]][3]
Also putting the code here for reference.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Body": "<p>Your CMK will expire in 30 days</p>",
"ReplyTo": "myemailaddress ",
"Subject": "Key Expiry",
"To": "myemailaddress"
},
"host": {
"connection": {
"name": "#parameters('$connections')['outlook']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"When_a_resource_event_occurs": {
"inputs": {
"body": {
"properties": {
"destination": {
"endpointType": "webhook",
"properties": {
"endpointUrl": "#{listCallbackUrl()}"
}
},
"filter": {
"includedEventTypes": [
"Microsoft.KeyVault.KeyNearExpiry"
]
},
"topic": "/subscriptions/<subscriptionid>/resourceGroups/pallabdev/providers/Microsoft.KeyVault/vaults/testhalvault"
}
},
"host": {
"connection": {
"name": "#parameters('$connections')['azureeventgrid_1']['connectionId']"
}
},
"path": "/subscriptions/#{encodeURIComponent('subscriptionId')}/providers/#{encodeURIComponent('Microsoft.KeyVault.vaults')}/resource/eventSubscriptions",
"queries": {
"x-ms-api-version": "2017-06-15-preview"
}
},
"splitOn": "#triggerBody()",
"type": "ApiConnectionWebhook"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureeventgrid_1": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/PallabDev/providers/Microsoft.Web/connections/azureeventgrid",
"connectionName": "azureeventgrid",
"connectionProperties": {
"authentication": {
"type": "ManagedServiceIdentity"
}
},
"id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/canadacentral/managedApis/azureeventgrid"
},
"outlook": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/PallabDev/providers/Microsoft.Web/connections/outlook",
"connectionName": "outlook",
"id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/canadacentral/managedApis/outlook"
}
}
}
}
}
The reason is that the triggerbody is null, it needs to be an array, if it is null, it will cause your error.
According to the discussion in the comment area, turning off Split On can avoid this error.

Azure service Bus get all queue message

i am using azure service bus queue. but i am not able to get all message from queue using "get all queue message (peek Lock): Microsoft built in api.
Is there any way to get all queue messages?
{
"$connections": {
"value": {
"servicebus_1": {
"connectionId": "/subscriptions/c776fec3-6aec-4722-b099-b054c267b240/resourceGroups/Plugin-Resource/providers/Microsoft.Web/connections/servicebus-3",
"connectionName": "servicebus-3",
"id": "-b054c267b240/providers/Microsoft.Web/locations/northcentralus/managedApis/servicebus"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Get_messages_from_a_queue_(peek-lock)": {
"inputs": {
"host": {
"api": {
"runtimeUrl": "https://logic-apis-northcentralus.azure-apim.net/apim/servicebus"
},
"connection": {
"name": "#parameters('$connections')['servicebus_1']['connectionId']"
}
},
"method": "get",
"path": "/#{encodeURIComponent('email-listener')}/messages/batch/peek",
"queries": {
"maxMessageCount": 20,
"queueType": "Main"
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 3
},
"type": "Recurrence"
}
}
}
}
thanks
The managed API by default returns 20 messages.
You'll need to manually increase the max message count if you have more than 20 messages in your queue.

Resources