I am currently developing an Azure Logic App that imports data by repeatedly performing HTTP POST requests. My backend has a property that may not be null or empty and because I don't want to change this I tried not including the property in the request body at all.
So for example
{
"id": "1234-5678",
"value": @{variables('myId')}
}
shall only be included in the body if the variable is neither null or empty. I tried adding an expression similar to
if(not(variables('myId')), '', concat(',{"id":"1234-5678","value": ', variables('myId'), '},'))
To the request body to make it more dynamic but now it says that the body is not valid anymore (invalid json). Is there anything else I could use? Using a condition and multiple different HTTP requests not not an option because I have multiple values that need this treatment.
CodePudding user response:
Yep, add it first and then remove it using a condition around the removeProperty
expression. You can also use the addProperty
approach but I went for the former. It really doesn't matter.
Load this definition into your tenant and you'll see it working.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Initialize_MyId": {
"inputs": {
"variables": [
{
"name": "MyId",
"type": "string"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Initialize_New_Payload": {
"inputs": {
"variables": [
{
"name": "New Payload",
"type": "object",
"value": "@if(equals(variables('MyId'), ''), removeProperty(variables('Payload'), 'value'), variables('Payload'))"
}
]
},
"runAfter": {
"Initialize_Payload": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_Payload": {
"inputs": {
"variables": [
{
"name": "Payload",
"type": "object",
"value": {
"id": "1234-5678",
"value": "@{variables('myId')}"
}
}
]
},
"runAfter": {
"Initialize_MyId": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Month",
"interval": 3
},
"recurrence": {
"frequency": "Month",
"interval": 3
},
"type": "Recurrence"
}
}
},
"parameters": {}
}
As a basic example, I have three steps ...
- Create a variable that stores a value for MyId
- Create a variable of type Object that stores the payload you spoke about in your question.
- Use a condition to remove the value property if the value of MyId is blank.
The specific expression in question is ...
if(equals(variables('MyId'), ''), removeProperty(variables('Payload'), 'value'), variables('Payload'))
Result Blank
Result NOT Blank
CodePudding user response:
I chose to solve this by sending multiple requests, first one for all properties that were already working and then one for each of those "special" properties. I know it will severely hurt the application's performance but as time became more of a concern I went with this anyways.
First, I create an object in the database with a POST request, parse it to JSON with the respective function, get the object id and then perform multiple PUT requests on it depending on which values I have. If my value exists I only add one property to the object and if the value does not exist I don't perform a request at all.