Search code examples
power-automate

How to create a formatted string using multiple fields from an array of objects


Say I have pulled a list of rows from an Excel connector, such that I'm passing around an array that looks like this:

[
  {
    "id": 3,
    "name": "John",
    "colour": "Red"
  }
  ,
  {
    "id": 5,
    "name": "Mark",
    "colour": "Black"
  }
]

And now I want to send an email which includes in the body:

  • John likes the colour Red
  • Mark likes the colour Black

How do I go about achieving that using the various built-in manipulation tools?

In C# I'd just be saying:

data.Select(r => $"* {r.name} likes the colour {r.colour}"), Environment.NewLine)

But

  1. there are split() and join() functions for expressions, but nothing like a select() AFAICT, and I can't see how to use the Select Action Block to solve this.
  2. I can't see a way to do Environment.NewLine at all?

Ideally I'd prefer a solution that doesn't involve using json(), nor explicitly looping through the array and manipulating individual variables those both seem super heavy-weight for such a trivial task, but if either of those are the only way to do it, then that would still be good to know.


Solution

    1. You'll need a simple string variable, let's be 'var1'
    2. I've put your demo array into a simple Compose action.
    3. Make a new 'Apply to each' container, where the input is your array (here: the 'Compose' action)
    4. Inside the Apply-to-each step, an 'Append to string variable':

    concat(item()?['name'],' likes the colour ',item()?['colour'],'.')

    1. Don't forget to add a simple Enter (carriage return) for the new line in the end of the 'Append to string variable' action enter image description here

    ((99. if it resolves your issue, please mark it as an answer. :) )