0

What I would like to do it if there is addOn array, I would like to append the word "xxx" to the end of the name.

Schema applied to message.
{
    "properties": {
        "appointment": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "lines": {
                    "items": {
                        "properties": {
                            "addOn": {
                                "items": {
                                    "properties": {
                                        "id": {
                                            "type": "integer"
                                        },
                                        "name": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "id",
                                        "name"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            },
                            "id": {
                                "type": "integer"
                            },
                            "price": {
                                "type": "integer"
                            }
                        },
                        "required": [
                            "id",
                            "price"
                        ],
                        "type": "object"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        },
        "messageId": {
            "type": "string"
        }
    },
    "type": "object"
}

Message 1
{
  "messageId": "11",
  "appointment": {
    "id": 22,
     "lines": [
      {
        "id": 33,
        "price": 125.0,
        "addOn": [
          {
            "id": 44,
            "name": "test"
          }
        ]
      }
    ]
  }
}

Message 2
{
  "messageId": "11",
  "appointment": {
    "id": 22,
     "lines": [
      {
        "id": 33,
        "price": 125.0
      }
    ]
  }
}

Message 1 works fine but whenever I try and use length or Parse Array I get a message that addOn is null.

How can I put a proper Condition express to not get any errors or do nothing when there is no addOn array.

Logic App. enter image description here

Message1 - Okay

Message2 - Error enter image description here

Code View. { "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "For_each": { "actions": { "For_each_2": { "actions": { "Condition_3": { "actions": {}, "expression": { "and": [ { "equals": [ "@empty(items('For_each_2'))", "True" ] } ] }, "runAfter": {}, "type": "If" } }, "foreach": "@items('For_each')['addOn']", "runAfter": {}, "type": "Foreach" } }, "foreach": "@body('Parse_JSON')?['appointment']?['lines']", "runAfter": { "Parse_JSON": [ "Succeeded" ] }, "type": "Foreach" }, "Parse_JSON": { "inputs": { "content": "@triggerBody()", "schema": { "properties": { "appointment": { "properties": { "id": { "type": "integer" }, "lines": { "items": { "properties": { "addOn": { "items": { "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": [ "id", "name" ], "type": "object" }, "type": "array" }, "id": { "type": "integer" }, "price": { "type": "integer" } }, "required": [ "id", "price" ], "type": "object" }, "type": "array" } }, "type": "object" }, "messageId": { "type": "string" } }, "type": "object" } }, "runAfter": {}, "type": "ParseJson" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": {}, "triggers": { "manual": { "inputs": { "schema": { "properties": { "appointment": { "properties": { "id": { "type": "integer" }, "lines": { "items": { "properties": { "addOn": { "items": { "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": [ "id", "name" ], "type": "object" }, "type": "array" }, "id": { "type": "integer" }, "price": { "type": "integer" } }, "required": [ "id", "price", "addOn" ], "type": "object" }, "type": "array" } }, "type": "object" }, "messageId": { "type": "string" } }, "type": "object" } }, "kind": "Http", "type": "Request" } } }, "parameters": {} }

Steve

Steve Moro
  • 83
  • 7

1 Answers1

1

I would like to append the word "xxx" to the end of the name.

For appending you can directly add while setting the "name" variable.

enter image description here

Alternatively, you can also use concat function as below.

concat(body('Parse_JSON')?['appointment']?['lines'][0]['addOn'][0]['name'],variables('xxx'))

enter image description here

where both cases would give the result as the following

enter image description here

How can I put a proper Condition express to not get any errors or do nothing when there is no addOn array.

You can do this in many ways where one of the workarounds would be using if the lines contain "addOn" or not.

"contains": ["@body('Parse_JSON')?['appointment']?['lines'][0]","addOn"]

enter image description here

MESSAGE1 RESULTS:

enter image description here

MESSAGE2 RESULTS:

enter image description here

You can reproduce the same in your logic app using the below codeview

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@variables('Message')",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Set_variable": {
                                "inputs": {
                                    "name": "Message",
                                    "value": {
                                        "appointment": {
                                            "id": "@body('Parse_JSON')?['appointment']?['id']",
                                            "lines": [
                                                {
                                                    "addOn": "addOn is NULL",
                                                    "id": "@items('For_each')?['id']",
                                                    "price": "@items('For_each')?['price']"
                                                }
                                            ]
                                        },
                                        "messageId": "@{body('Parse_JSON')?['messageId']}"
                                    }
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "For_each_2": {
                                    "actions": {
                                        "Set_variable_2": {
                                            "inputs": {
                                                "name": "Message",
                                                "value": {
                                                    "appointment": {
                                                        "id": "@body('Parse_JSON')?['appointment']?['id']",
                                                        "lines": [
                                                            {
                                                                "addOn": [
                                                                    {
                                                                        "id": "@items('For_each_2')?['id']",
                                                                        "name": "@{concat(body('Parse_JSON')?['appointment']?['lines'][0]['addOn'][0]['name'],variables('xxx'))}"
                                                                    }
                                                                ],
                                                                "id": "@items('For_each')?['id']",
                                                                "price": "@items('For_each')?['price']"
                                                            }
                                                        ]
                                                    },
                                                    "messageId": "@{body('Parse_JSON')?['messageId']}"
                                                }
                                            },
                                            "runAfter": {},
                                            "type": "SetVariable"
                                        }
                                    },
                                    "foreach": "@items('For_each')['addOn']",
                                    "runAfter": {},
                                    "type": "Foreach"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "not": {
                                        "contains": [
                                            "@body('Parse_JSON')?['appointment']?['lines'][0]",
                                            "addOn"
                                        ]
                                    }
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@body('Parse_JSON')?['appointment']?['lines']",
                "runAfter": {
                    "Initialize_variable_xxx": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Message",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_xxx": {
                "inputs": {
                    "variables": [
                        {
                            "name": "xxx",
                            "type": "string",
                            "value": "xxx"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@triggerBody()",
                    "schema": {
                        "properties": {
                            "appointment": {
                                "properties": {
                                    "id": {
                                        "type": "integer"
                                    },
                                    "lines": {
                                        "items": {
                                            "properties": {
                                                "addOn": {
                                                    "items": {
                                                        "properties": {
                                                            "id": {
                                                                "type": "integer"
                                                            },
                                                            "name": {
                                                                "type": "string"
                                                            }
                                                        },
                                                        "required": [
                                                            "id",
                                                            "name"
                                                        ],
                                                        "type": "object"
                                                    },
                                                    "type": "array"
                                                },
                                                "id": {
                                                    "type": "integer"
                                                },
                                                "price": {
                                                    "type": "integer"
                                                }
                                            },
                                            "required": [
                                                "id",
                                                "price"
                                            ],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            },
                            "messageId": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {},
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "appointment": {
                                "properties": {
                                    "id": {
                                        "type": "integer"
                                    },
                                    "lines": {
                                        "items": {
                                            "properties": {
                                                "addOn": {
                                                    "items": {
                                                        "properties": {
                                                            "id": {
                                                                "type": "integer"
                                                            },
                                                            "name": {
                                                                "type": "string"
                                                            }
                                                        },
                                                        "required": [
                                                            "id",
                                                            "name"
                                                        ],
                                                        "type": "object"
                                                    },
                                                    "type": "array"
                                                },
                                                "id": {
                                                    "type": "integer"
                                                },
                                                "price": {
                                                    "type": "integer"
                                                }
                                            },
                                            "required": [
                                                "id",
                                                "price",
                                                "addOn"
                                            ],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            },
                            "messageId": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18