Append result in Azure Logic Apps

SATISH KHASHABA JADHAV 0 Reputation points
2024-01-07T03:59:22.62+00:00

I have below json input to logic apps

[    {

        "ACTIVITYID": "0002868073",
        "STATUS": "Released",
        "COMMENTS":[
            {
               "COMMENT":"Comment_1"
            },
            {
                "COMMENT":"Comment_2"
             }
        ]
    },{
        "ACTIVITYID": "0002868071",
        "STATUS": "Approved",
        "COMMENTS":[
]            {
               "COMMENT":"Comment_3"
            },
            {
                "COMMENT":"Comment_4"
             }
        ]
    }
]


I want output like

0002868073 Released Comment_1 Comment_2

0002868071 Approved Comment_3 Comment_4

Here is my logic apps code view

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each_-_ActivityLoop": {
                "actions": {
                    "Append_to_string_variable_2": {
                        "inputs": {
                            "name": "FinalComment",
                            "value": "@variables('AppendInnerComment')"
                        },
                        "runAfter": {
                            "For_each_-_CommentsLoop": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToStringVariable"
                    },
                    "For_each_-_CommentsLoop": {
                        "actions": {
                            "Append_to_string_variable": {
                                "inputs": {
                                    "name": "InnerComment",
                                    "value": "@items('For_each_-_CommentsLoop')['COMMENT']"
                                },
                                "runAfter": {},
                                "type": "AppendToStringVariable"
                            },
                            "Append_to_string_variable_3": {
                                "inputs": {
                                    "name": "AppendInnerComment",
                                    "value": "@variables('InnerComment')"
                                },
                                "runAfter": {
                                    "Append_to_string_variable": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "AppendToStringVariable"
                            }
                        },
                        "foreach": "@items('For_each_-_ActivityLoop')['COMMENTS']",
                        "runAfter": {},
                        "type": "Foreach"
                    },
                    "Set_variable_2": {
                        "inputs": {
                            "name": "FinalOutput",
                            "value": "@{items('For_each_-_ActivityLoop')['ACTIVITYID']}@{items('For_each_-_ActivityLoop')['STATUS']}@{variables('FinalComment')}"
                        },
                        "runAfter": {
                            "Append_to_string_variable_2": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_FinalComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FinalComment",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable_-_AppendInnerComment": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_FinalOutput": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FinalOutput",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_FinalComment": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_InnerComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "InnerComment",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_FinalOutput": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_-_AppendInnerComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "AppendInnerComment",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@triggerBody()",
                    "schema": {
                        "items": {
                            "properties": {
                                "ACTIVITYID": {
                                    "type": "string"
                                },
                                "COMMENTS": {
                                    "items": {
                                        "properties": {
                                            "COMMENT": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "COMMENT"
                                        ],
                                        "type": "object"
                                    },
                                    "type": "array"
                                },
                                "SAPUSERID": {
                                    "type": "string"
                                },
                                "STATUS": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "ACTIVITYID",
                                "STATUS",
                                "SAPUSERID",
                                "COMMENTS"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "Initialize_InnerComment": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "POST",
                    "schema": {
                        "items": {
                            "properties": {
                                "ACTIVITYID": {
                                    "type": "string"
                                },
                                "COMMENTS": {
                                    "items": {
                                        "properties": {
                                            "COMMENT": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "COMMENT"
                                        ],
                                        "type": "object"
                                    },
                                    "type": "array"
                                },
                                "SAPUSERID": {
                                    "type": "string"
                                },
                                "STATUS": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "ACTIVITYID",
                                "STATUS",
                                "SAPUSERID",
                                "COMMENTS"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}


However, I get the output

0002868073 Released Comment_1 Comment_1 Comment_2

0002868071 Approved Comment_1Comment_1Comment_2Comment_1Comment_1Comment_2Comment_1Comment_2Comment_3Comment_1Comment_2Comment_3Comment_4

Please advice to get the desired result.

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2024-01-07T10:31:09.04+00:00

    Your output is repeating comments because the variables are not being reset or used appropriately within the loops. It appears that there is a problem with the way you are handling the string variables and appending comments in your current setup.

    please try the following

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "For_each_-_ActivityLoop": {
                    "actions": {
                        "For_each_-_CommentsLoop": {
                            "actions": {
                                "Append_to_string_variable": {
                                    "inputs": {
                                        "name": "FinalComment",
                                        "value": "@items('For_each_-_CommentsLoop')['COMMENT']"
                                    },
                                    "runAfter": {},
                                    "type": "AppendToStringVariable"
                                }
                            },
                            "foreach": "@items('For_each_-_ActivityLoop')['COMMENTS']",
                            "runAfter": {},
                            "type": "Foreach"
                        },
                        "Set_variable_2": {
                            "inputs": {
                                "name": "FinalOutput",
                                "value": "@{items('For_each_-_ActivityLoop')['ACTIVITYID']}@{items('For_each_-_ActivityLoop')['STATUS']}@{variables('FinalComment')}"
                            },
                            "runAfter": {
                                "For_each_-_CommentsLoop": [
                                    "Succeeded"
                                ]
                            },
                            "type": "SetVariable"
                        }
                    },
                    "foreach": "@body('Parse_JSON')",
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                // ... other actions (initialization, parsing)
            },
            // ... rest of the workflow
        }
    }
    
    

  2. Chakaravarthi Rangarajan Bhargavi 1,115 Reputation points MVP
    2024-01-07T11:20:36.91+00:00

    Hi SATISH KHASHABA JADHAV,

    I went through the Json code and have few observations. It looks like there's a small change needs to be done in your Logic Apps workflow which is causing the comments to be appended multiple times. Observations that I have:

    1. Unnecessary "Initialize_variable_-_AppendInnerComment" action which can be removed.
    2. Instead of using multiple variables for each comment and appending, you can use expressions directly within the "Set_variable_2" action to construct the output string.
    3. The main change is in the second activity where an empty array is used for COMMENTS, as per your original JSON structure.
    Declarations:
    type Comment struct {
     	COMMENT string } 
    type Activity struct {
     	ACTIVITYID string
     	STATUS     string
     	COMMENTS   []Comment }
    
    Inside main: 
    data := []Activity{
     		{
     			ACTIVITYID: "0002868073",
     			STATUS:     "Released",
     			COMMENTS: []Comment{
     				{COMMENT: "Comment_1"},
     				{COMMENT: "Comment_2"},
     			},
     		},
     		{
     			ACTIVITYID: "0002868071",
     			STATUS:     "Approved",
     			COMMENTS: []Comment{
     				{COMMENT: "Comment_3"},
     				{COMMENT: "Comment_4"},
     			},
     		},
     	}
      	for _, activity := range data {
     		comments := []string{}
     		for _, comment := range activity.COMMENTS {
     			comments = append(comments, comment.COMMENT)
     		} 		
    result := fmt.Sprintf("%s %s %s", activity.ACTIVITYID, activity.STATUS, strings.Join(comments, " ")) 		fmt.Println(result)
     	}
    

    Please try out these steps It should still achieve the desired result with a cleaner structure. Please let me know if the attached snippet helps you!

    Good luck with your exploration and development efforts, and feel free to reach out through this comment if you need further assistance.

    Best regards,

    Chakravarthi Rangarajan Bhargavi

    -Please kindly accept the answer and vote 'Yes' if you feel helpful to support the community, thanks a lot.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.