Mapping request body fields in logic apps - json

We have configured common alert schema for alerts and we are using a ticketing software and whenever we receive alert it should create a ticket.
In the logic app I have to make Post API call for creation of ticket with following json object, some fields are hard coded values:
{
"subject": "",
"Id": "123456789", // Hard code value
"priority": "",
"email": "test#test.com", // Hard code value
"status": "Open" // Hard code value
}
Parse Json sample payload schema for Alert:
{
"data": {
"alertContext": {
},
},
"customProperties": null,
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
}
},
"schemaId": "test"
}
I have to map "subject and "priority" fields with alert json object "description" and "severity":
subject-->description
priority --> severity // sev0 =high ,sev1=medium, sev2 =low
How can I achieve this using logic app?

After Parse JSON You can directly map its objects in the compose connector with the required fields. Below is my logic app flow.
You can use the below Code view to reproduce the same in your Logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"customProperties": null,
"data": {
"alertContext": {}
},
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
},
"schemaId": "test"
},
"runAfter": {},
"type": "Compose"
},
"Compose_2": {
"inputs": {
"Id": "123456789",
"email": "test#test.com",
"priority": "#{body('Parse_JSON')?['essentials']?['severity']}",
"status": "Open",
"subject": "#{body('Parse_JSON')?['essentials']?['description']}"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"customProperties": {},
"data": {
"properties": {
"alertContext": {
"properties": {},
"type": "object"
}
},
"type": "object"
},
"essentials": {
"properties": {
"alertContextVersion": {
"type": "string"
},
"alertId": {
"type": "string"
},
"alertRule": {
"type": "string"
},
"description": {
"type": "string"
},
"severity": {
"type": "string"
}
},
"type": "object"
},
"schemaId": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Related

Azure Function App new JSON with only a few properties

I am struggling of finding a feasible solution for my Azure Logic App.
A HTTP Request-action call will list the virtual networks of an Azure subscription.
The response look somehow like this:
{
"value": [
{
"name": "virtualNetworkName1",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
"etag": "W/\"11111-1111-111\"",
"type": "Microsoft.Network/virtualNetworks",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "111-1111-11111",
"addressSpace": {
"addressPrefixes": [
"192.168.0.0/25"
]
}
}
},
{
"name": "virtualNetworkName2",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
"etag": "W/\"22222-2222-222\"",
"type": "Microsoft.Network/virtualNetworks",
"location": "westeurope",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "222-2222-22222",
"addressSpace": {
"addressPrefixes": [
"192.168.1.0/24"
]
}
}
}
]
}
The resonse has even more properties which aren't necessary.
Regarding to this, I would like to use the HTTP Response-action in a JSON format with only a few properties:
Name
Id
Location
Like this:
[
{
"name": "virtualNetworkName1",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
"location": "eastus"
},
{
"name": "virtualNetworkName2",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
"location": "westeurope"
}
]
Is it possible to realize it with only Logic App native actions?
I have reproduced in my environment and got expected results as below:
Firstly, I have initialized your output in a variable and then used parse Json and compose to get required output:
Parse Json Schema:
{
"properties": {
"value": {
"items": {
"properties": {
"id": {
"type": "string"
},
"location": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
Output:
First json Output:
Second one:
Code view of Logic app:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"value": [
{
"etag": "W/\"11111-1111-111\"",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
"location": "eastus",
"name": "virtualNetworkName1",
"properties": {
"addressSpace": {
"addressPrefixes": [
"192.168.0.0/25"
]
},
"provisioningState": "Succeeded",
"resourceGuid": "111-1111-11111"
},
"type": "Microsoft.Network/virtualNetworks"
},
{
"etag": "W/\"22222-2222-222\"",
"id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
"location": "westeurope",
"name": "virtualNetworkName2",
"properties": {
"addressSpace": {
"addressPrefixes": [
"192.168.1.0/24"
]
},
"provisioningState": "Succeeded",
"resourceGuid": "222-2222-22222"
},
"type": "Microsoft.Network/virtualNetworks"
}
]
},
"runAfter": {},
"type": "Compose"
},
"For_each": {
"actions": {
"Compose_2": {
"inputs": {
"id": "#items('For_each')?['id']",
"location": "#items('For_each')?['location']",
"name": "#items('For_each')?['name']"
},
"runAfter": {},
"type": "Compose"
}
},
"foreach": "#body('Parse_JSON')?['value']",
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Foreach"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"value": {
"items": {
"properties": {
"id": {
"type": "string"
},
"location": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
Now you will get required fields as I have got.

Parsing JSON array to individual variables in Azure logic apps

I am trying to write a logic app to parse a Json Object and Update salesforce record. I am pretty new to both Salesforce and Azure logic apps, so I am trying to figure this out. Below is my Json File
{
"ContactId": null,
"Email": "asong#uog.com",
"IsInternalUpdate": false,
"Preferences": [
{
"PrefCode": "EmailOptIn",
"CurrentValue": "Yes",
"Locale": "en-US"
},
{
"PrefCode": "MobilePhone",
"CurrentValue": "1234567890",
"Locale": "en-US"
},
{
"PrefCode": "SMSOptIn",
"CurrentValue": "Yes",
"Locale": "en-US"
},
{
"PrefCode": "ProductTrends",
"CurrentValue": "ProductTrends,OffersPromotions",
"Locale": "en-US"
},
]
}
Based on email value, I need to update a custom object in Salesforce. From the preference array, Prefcode value maps to a field in Salesforce and Current value maps to field value. i.e below snippet translates to set the value for EmailOptIn field in Salesforce to "Yes"
{
"PrefCode": "EmailOptIn",
"CurrentValue": "Yes",
"Locale": "en-US"
}
So far, I was able to pass hardcoded values and successfully update salesforce record from logic app.
I am trying to set individual variables for each field, so that I can pass it directly to salesforce. I have two issues that I am running into
What is the best way to capture the field value mapping?
I have couple of fields that allow multi select, how do I set the multiselect values. Below is an example
{
"PrefCode": "ProductTrends",
"CurrentValue": "ProductTrends,OffersPromotions",
"Locale": "en-US"
}
Below is my logic app structure
1
2
What is the best way to capture the field value mapping?
I could able to achieve your requirement by constructing JSON with required details and used parse JSON step again. Below is the flow of the logic app that worked for me.
I have couple of fields that allow multi select, how do I set the multiselect values. Below is an example
I have converted the ProductTrends into array and retrieved each item using its index as below.
array(split(body('Parse_JSON_2')?['ProductTrends'],','))[0]
array(split(body('Parse_JSON_2')?['ProductTrends'],','))[1]
Alternatively, if it is possible to make things work from the target end for multiple values, you can send in array or string directly to salesforce.
Below is the complete JSON of my logic app.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"ContactId": null,
"Email": "asong#uog.com",
"IsInternalUpdate": false,
"Preferences": [
{
"CurrentValue": "Yes",
"Locale": "en-US",
"PrefCode": "EmailOptIn"
},
{
"CurrentValue": "1234567890",
"Locale": "en-US",
"PrefCode": "MobilePhone"
},
{
"CurrentValue": "Yes",
"Locale": "en-US",
"PrefCode": "SMSOptIn"
},
{
"CurrentValue": "ProductTrends,OffersPromotions",
"Locale": "en-US",
"PrefCode": "ProductTrends"
}
]
},
"runAfter": {},
"type": "Compose"
},
"Compose_2": {
"inputs": "#array(split(body('Parse_JSON_2')?['ProductTrends'],','))[0]",
"runAfter": {
"Parse_JSON_2": [
"Succeeded"
]
},
"type": "Compose"
},
"For_each": {
"actions": {
"Append_to_string_variable": {
"inputs": {
"name": "Preferences",
"value": "\"#{items('For_each')?['PrefCode']}\":\"#{items('For_each')?['CurrentValue']}\",\n"
},
"runAfter": {},
"type": "AppendToStringVariable"
}
},
"foreach": "#body('Parse_JSON')?['Preferences']",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "Preferences",
"type": "string"
}
]
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"ContactId": {},
"Email": {
"type": "string"
},
"IsInternalUpdate": {
"type": "boolean"
},
"Preferences": {
"items": {
"properties": {
"CurrentValue": {
"type": "string"
},
"Locale": {
"type": "string"
},
"PrefCode": {
"type": "string"
}
},
"required": [
"PrefCode",
"CurrentValue",
"Locale"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
},
"Parse_JSON_2": {
"inputs": {
"content": "#concat('{',slice(variables('Preferences'),0,lastIndexOf(variables('Preferences'),',')),'}')",
"schema": {
"properties": {
"EmailOptIn": {
"type": "string"
},
"MobilePhone": {
"type": "string"
},
"ProductTrends": {
"type": "string"
},
"SMSOptIn": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
After following the above process, I could able to get the preferences values individually.

whatsapp cloud api template object issue to send from postman

I'm trying to send the whatsapp cloud template from postman.
I created a template in whatsapp cloud with header media image,
body content,footer and two buttons.
the response of the templates when i use get api is as below
{
"name": "trns_btn_img_header_XXX",
"components": [
{
"type": "HEADER",
"format": "IMAGE",
"example": {
"header_handle": [
"https://img.url.com"
]
}
},
{
"type": "BODY",
"text": "Body message"
},
{
"type": "FOOTER",
"text": "ftr optioal"
},
{
"type": "BUTTONS",
"buttons": [
{
"type": "QUICK_REPLY",
"text": "qrbtnone"
},
{
"type": "QUICK_REPLY",
"text": "qrbtntwo"
}
]
}
],
"language": "en_US",
"status": "APPROVED",
"category": "TRANSACTIONAL",
"id": "17XX209448XXXXXX"
}
I tried the template json object in postman is as below
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{message_to}}",
"type": "template",
"template": {
"name": "trns_btn_img_header_XXX",
"language": {
"code": "en_US"
},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {
"link": "https://img.jpg.com"
}
}
]
},
{
"type": "body",
"parameters": [
{
"type": "text",
"text": "Body message from pm"
},
]
},
{
"type": "footer",
"parameters": [
{
"type": "text",
"text": "footer message from pm"
},
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": "0",
"parameters": [
{
"type": "text",
"text": "btnone"
}
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": "1",
"parameters": [
{
"type": "text",
"text": "btntwo"
}
]
}
]
}
}
the response error is "error": {
"message": "(#132000) Number of parameters does not match the expected number of params"
Make sure and correct the below things in send message endpoint request,
Don't need to pass body component if there are no parameters in the body text, if there are parameters then you need to pass only that parameters text in the separate object of parameters in index order
Don't need to pass the footer component because it is static when you create a template
the quick_reply button type, use type as "payload" instead of "text" in parameters
{
"type": "button",
"sub_type": "quick_reply",
"index": "1",
"parameters": [
{
"type": "payload",
"payload": "btntwo"
}
]
}
Below object worked for me
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{message_to}}",
"type": "template",
"template": {
"name": "trns_btn_img_header_XXX",
"language": {
"code": "en_US"
},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {
"link": "https://www.w3schools.com/html/pic_trulli.jpg"
}
}
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": "0",
"parameters": [
{
"type": "payload",
"payload": "btntwo"
}
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": "1",
"parameters": [
{
"type": "payload",
"payload": "btnto"
}
]
}
]
}
}

Editing json content with azure logic app

I want to create a logic app which replaces the value of resourcename inside an alert json structure received by a logic app. The alarm structure is as follows:
{
"schemaId": "AzureMonitorMetricAlert",
"data": {
"version": "2.0",
"properties": null,
"status": "Deactivated",
"context": {
"timestamp": "2021-08-21T01:43:09.2000007Z",
"id": "/subscriptions/<subscription_id>/resourceGroups/<my_resource_group>/providers/microsoft.insights/metricAlerts/teste%20-%20vpnp2s%20count",
"name": "teste - vpnp2s count",
"description": "",
"conditionType": "SingleResourceMultipleMetricCriteria",
"severity": "3",
"condition": {
"windowSize": "PT1M",
"allOf": [
{
"metricName": "P2SConnectionCount",
"metricNamespace": "Microsoft.Network/p2sVpnGateways",
"operator": "GreaterThanOrEqual",
"threshold": "1",
"timeAggregation": "Total",
"dimensions": [],
"metricValue": 0,
"webTestName": null
}
]
},
"subscriptionId": "<subscription_id>",
"resourceGroupName": "<my_resource_group>",
"resourceName": "some_resource_name",
"resourceType": "Microsoft.Network/p2sVpnGateways",
"resourceId": "/subscriptions/<subscription_id>/resourceGroups/<my_resource_group>/providers/Microsoft.Network/p2sVpnGateways/<p2svpngatewayid>",
"portalLink": "https://portal.azure.com/#resource/subscriptions/<subscription_id>/resourceGroups/<my_resource_group>/providers/Microsoft.Network/p2sVpnGateways/<theresourceid>"
}
}
}
I've been trying all sorts of mix with 'set variable', 'initialize variable', 'compose' blocks, but still no luck. Anyone has a clue?
Based on the above requirement i have created the logic app which replaces the resource name using HTTP request & replace actions.
Below is the code view of my logic app :
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"ConversionToString": {
"inputs": {
"variables": [
{
"name": "StringConversion",
"type": "string",
"value": "#{json(string(triggerBody()))}"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"FInalJsonOutput": {
"inputs": {
"variables": [
{
"name": "JsonOutput",
"type": "object",
"value": "#json(variables('replaceResouceName'))"
}
]
},
"runAfter": {
"ReplaceResourceName": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"ReplaceResourceName": {
"inputs": {
"variables": [
{
"name": "replaceResouceName",
"type": "string",
"value": "#{replace(variables('StringConversion'),'some_resource_name','newresource')}"
}
]
},
"runAfter": {
"ConversionToString": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {
"properties": {
"data": {
"properties": {
"context": {
"properties": {
"condition": {
"properties": {
"allOf": {
"items": {
"properties": {
"dimensions": {
"type": "array"
},
"metricName": {
"type": "string"
},
"metricNamespace": {
"type": "string"
},
"metricValue": {
"type": "integer"
},
"operator": {
"type": "string"
},
"threshold": {
"type": "string"
},
"timeAggregation": {
"type": "string"
},
"webTestName": {}
},
"required": [
"metricName",
"metricNamespace",
"operator",
"threshold",
"timeAggregation",
"dimensions",
"metricValue",
"webTestName"
],
"type": "object"
},
"type": "array"
},
"windowSize": {
"type": "string"
}
},
"type": "object"
},
"conditionType": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"portalLink": {
"type": "string"
},
"resourceGroupName": {
"type": "string"
},
"resourceId": {
"type": "string"
},
"resourceName": {
"type": "string"
},
"resourceType": {
"type": "string"
},
"severity": {
"type": "string"
},
"subscriptionId": {
"type": "string"
},
"timestamp": {
"type": "string"
}
},
"type": "object"
},
"properties": {},
"status": {
"type": "string"
},
"version": {
"type": "string"
}
},
"type": "object"
},
"schemaId": {
"type": "string"
}
},
"type": "object"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
Here is the sample output for reference post running the above logic app

How do parse string as JSON in Logic App?

I have JSON below which I receive from external entity. As you can see requestbody parameter is appearing as string even though it's JSON. So how do I unescape it so I can correctly parse it downstream?
{
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
}
Use a Parse JSON Action as shown below:
Content:
{
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
}
Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"emailaddress": {
"type": "string"
},
"requestbody": {
"type": "string"
}
},
"required": [
"emailaddress",
"requestbody"
],
"type": "object"
}
Initialize Variable
-Name = Variable Name
-Type = Object
-Value = json(body('Parse_JSON')['requestbody'])
Now you can extract the properties of your Json string as shown below:
variables('jsonobj')?['Properties']
Full Code view of my sample:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "jsonobj",
"type": "Object",
"value": "#json(body('Parse_JSON')['requestbody'])"
}
]
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Parse_JSON": {
"inputs": {
"content": {
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
},
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"emailaddress": {
"type": "string"
},
"requestbody": {
"type": "string"
}
},
"required": [
"emailaddress",
"requestbody"
],
"type": "object"
}
},
"runAfter": {},
"type": "ParseJson"
},
"Response": {
"inputs": {
"body": "#variables('jsonobj')?['Properties']",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
}
}
The easiest way is to use this expression:
#json(outputs('Mock_example_data').requestbody)
Below is an example using a Compose action to mock up your data and another Compose action as a simple proof of concept.