September 24, 2021

Power Automate: Convert an HTML table into a JSON

Some time ago, I checked some posts in the Power Automate Community, and this question popped up. It’s quite an interesting question, but it’s super hard to explain in an answer, so I decided to build a template to do it. Convert an HTML table in a JSON is not something that happens every day, but it’s an interesting challenge.

Convert an HTML table into a JSON

This template will enable you to convert an HTML table tag into JSON without using premium connectors.

If you want only to get the HTML values from a tag, I have another template to do that.
I also have instructions on how it works here.

Find HTML tag value

This template will enable you to get the value inside an HTML tag. More details about it are here.

 

This is the first version of this template. It’s not fully optimized, nor it supports all HTML combinations or tags. If something doesn’t work as expected, please send me an email with the HTML. I would recommend sending it as a text attachment.

I’ll use it to improve the template for everyone and hopefully create something useful to as many people as possible.

The template

The template is the biggest one I’ve created yet. It’s quite complex and too long to explain it here, but I’ve done what I always do. I’ve added notes in each action so that you know what each one is doing. I also included scope actions and group actions to make it easier to understand and deal with errors.

No premium connectors:
As always, I won’t use premium connectors. I nothing against them, but since many don’t have access, using them would exclude many people from using this template, and I don’t want that.

If you don’t know how to import the template, I have instructions to do it here, but in this case, we’ll be importing a whole solution (I’ll write detailed instructions on how to do that soon).

The HTML

Parsing HTML is incredibly complex, especially now that the complexity of anything that uses HTML has increased exponentially. I needed to make some decisions on how things would be parsed:

  1. All HTML attributes will be ignored. Attributes are everything inside tags, like <a href="">. In this case, “href” is an attribute.
  2. I will support column tags. If the HTML contains a <th> tag then I’ll use them as the JSON’s keys.
  3. If I don’t have the <th> tag, I’ll generate sequential key columns. For example Column_1.
  4. Only support one table. If the HTML contains multiple table tags, I’ll ignore them.
  5. I will only parse things between the <table>and </table> tags.
  6. I need at least one column row and one row or two rows. Otherwise, I’ll stop because without information, the process doesn’t make sense to continue.

Even with these decisions, we have a good compromise between usability and complexity of the Flow. I believe that all these decisions won’t take anything from the final result, but there’s always room to improve.

The return

The Flow will always return two parameters:

  1. Error
  2. Output

I like to do the returns like this because you can check if there is an error and what error it is. If the error is empty, you know what the output has, something that you can use.

Child Flows

I have a second template that you may use. You provide it with HTML, and it will find the first tag and return its value. For example:

<a href="">value</a>

it would return

value

Additionally, you can remove the spaces and other breaks to have a “clean” value. I wrote an article to get the HTML tag value and explain in detail how to do it.

<Insert template here>

The issue is that, at the time of writing this article, Power Automate doesn’t support importing directly to solutions. It doesn’t allow you to import templates that have child Flows since they can only be imported to solutions. So we have an issue. To fit it, we have two solutions:

  1. Transform the “Run Child Flow” action into an “HTTP” action. Unfortunately, this is not a good solution for everyone since the HTTP action is a premium action.
  2. Duplicate the actions each time we need to use them. This is a waste of resources and harder to maintain, but it’s the issue when generating templates to distribute offline. You have to go around the limitations.

We also have the issue that we can’t add Instant Flows with the “Manually trigger a Flow” action, so we’re stuck because we can’t import templates to solutions nor templates with child Flows outside the solutions.

So we need to do something different. We need to export and import a full solution. I’ll provide detailed instructions in the future, but for now, you’ll need to use the package and the “Import” feature in the solution. To do that:

  1. Go to solutions and select “Import.”
  1. Next, select the zip file and press “next.”
  2. Fill in the values, and you’ll get a solution as follows.

Final thoughts

I have to say that building this template was an awesome challenge, and I’m happy with the result, although I can surely upgrade it to be a LOT better. It’s also the first time I provide an entire solution that you can use and not only one Flow. This is the best way for you to have a “package” that you can use that is “ready to go.”

In the meantime, I hope you enjoy it, and please provide feedback.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other Power Automate-related articles here.

Photo by Veronika FitArt on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: