0

I am new to MS Flow, I am creating an approval process where I have to send data in a table.

I have an array, which is

[
  {
    "Header": [
      "Username",
      "Hname",
      "RE"
    ],
    "Data": [
      "aa",
      "bb",
      "cc"
    ]
  },
  {
    "Header": [
      "Username",
      "Hname",
      "RE"
    ],
    "Data": [
      "aa11",
      "bb11",
      "cc33"
    ]
  }
]

How can I create a Markdown table from the above array to get the below table?

|Header  |Data |

|Username|aa   |
|Hname   |bb   |
|RE      |cc   |
|Username|aa1  |
|Hname   |bb2  |
|RE      |cc3  |

 Error

I have passed the above array from the select output ie. body('select') to foreach action, and foreach output ie. items(apply_to_each) to parse_JSON. Below is the output from Parse_JSON.

{
  "Header": [
    "Username",
    "Hostname",
    "RE"
  ],
  "Data": [
    "aa",
    "bb",
    "cc"
  ]
}
1
  • 1
    The array given has an extra comma (,) in sub-arrays. You can use Parse JSON as @Ganesh suggested and create HTML table. However, you can also manually create HTML Table by using [Apply to each] action and string functions in Expressions Dec 17 '20 at 17:04
2

Since this a complex array, I used a combination of [Parse JSON] and [Apply to each] actions, and string functions to generate the HTML table to get the output as requested.

See the screenshots and explanation below.

enter image description here

varArray - a string variable, contains the array string that has been provided.

varHTMLTable - a string variable, initialized with a Table layout with some styles. See Expression 1.

Expression 1

<!DOCTYPE html>
<html>
<head>
<style>
th, td {
  border-left: 1px solid black;
  padding: 5px;
  width:200px;
}
th {text-align:left; }

</style>
</head>
<body>

<table style="width:50%">
  <tr>
    <th>Header</th>
    <th>Data</th> 
    <th></th>
  </tr>
<!--add a blank row between [Header] and [Data] Rows -->
  <tr>
    <td style="border:0px;">&nbsp;</td>
    <td style="border:0px;">&nbsp;</td>
    <td style="border:0px;">&nbsp;</td>
  </tr>

Detail 1

enter image description here

Expression 2

concat('<tr><td>', items('Apply_to_each')['Header']?[0], '</td><td>', items('Apply_to_each')['Data']?[0], '</td><td> </td></tr>',

'<tr><td>', items('Apply_to_each')['Header']?[1], '</td><td>', items('Apply_to_each')['Data']?[1], '</td><td> </td></tr>',

'<tr><td>', items('Apply_to_each')['Header']?[2], '</td><td>', items('Apply_to_each')['Data']?[2], '</td><td> </td></tr>'

)

Detail 2

enter image description here

The Final output seen in the email body

Note: Table styles can be adjusted as needed.

enter image description here


Updated for a Markdown Table

The process is the same to generate a Markdown Table. However, for adding a line break, only </br> worked for me.

Initialize a string variable, varMarkdownTable, with the table header

concat('## Markdown Table </br>', '|  Header | Data |</br>', '| :---------: | :---------: |</br>', '|  |  |</br>')

Within Apply to each, using [Append to string variable], append the table body string to varMarkdownTable variable

concat('| ', items('Apply_to_each')['Header']?[0], ' | ', items('Apply_to_each')['Data']?[0], ' |</br>', '| ', items('Apply_to_each')['Header']?[1], ' | ', items('Apply_to_each')['Data']?[1], ' |</br>', '| ', items('Apply_to_each')['Header']?[2], ' | ', items('Apply_to_each')['Data']?[2], '|</br>')

Generated Markdown Table as seen in outlook email

enter image description here

Copy the the output and verify using Visual Studio Code or MarkdownLivePreview

enter image description here

2
  • Hi @Matiur Rahman, I want to create a Markdown table in 'Start and wait for an approval' Action, not HTML table.
    – Riya
    Jan 17 at 8:38
  • 1
    Updated my original post showing how to build Markdown string in Power Automate. Let me know if you have any questions. Jan 17 at 20:31
1

You can use Parse JSON and then Select action to selecting specific properties you want to add to table.

Then you need to use Create HTML Table action in Power Automate to generate table from JSON data.

Check below references for detailed information:

  1. Create And Format HTML Table Using Microsoft Flow
  2. Handling JSON in Microsoft Flow
  3. How to use Select to simplify Create-HTML-Table
1
  • Hi @ganesh, I have created the above array from the select action. Basically, the header contains the key and the data contains the value. I have to create a markdown table for the approval email. I have edited the question please have a look.
    – Riya
    Dec 19 '20 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.