This is a note to myself. I read in some of the forums and in Azure Logic Apps user voice where people have asked for how to aggregate a value from an array of messages into a single message. For example, how to aggregate all tweets from a certain hashtag into a single message. Or how to concatenate/batch a value from the array of object into a single value. If it’s a C# project, one of the ways to achieve this, we would have used a global variable and appended the value with the loop, we are interested in, like this..
for (int i = 0; i <= TweetResultArray.Count; i++) { GobalVariable = GobalVariable +", "+ TweetResultArray.TweetText; }//GobalVariable after the loop will have the concatenate value of tweet text
But in Logic Apps, in the absence of global variable as of now, how to achieve the same. Let’s see it with an example.
If I want to aggregate all the tweets from the hashtag #LogicApps, you can do the logic apps like.
I’ll explain only the core part of interest.
- Added a “For each” action, to loop through the array of objects. And used a “Compose” action within the “For each” action to exact out the interested value from the array of objects. This action outputted array of tweets a string (or the value I am interested from the complex object/Tweet collection)
- Next action, added another “Compose” action outside the for-each and used the function “@join()” as @join(outputs(‘Compose’),’, ‘). This joined the output from the previous compose action.
This outputted tweet text with hashtag #LogicApps, delimited with a comma (,)
By using the actions as specified here, but without the @join(), we will get the output as an array of string as
[“tweet1”,”tweet2”,”tweet3”]
With @join(), the output would be a aggregated string as:
“tweet1, tweet2, tweet3”
Hope it helps.
Leave a Reply