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
}
}