November 6, 2024

How to convert an HTML table into a JSON in Power Automate?

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.

Find HTML tag value

This template will enable you to get the value inside an HTML tag. More details about it are 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 have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

7 thoughts on “How to convert an HTML table into a JSON in Power Automate?

  1. Hello Manuel, This looks to be exactly what I need but I have had to build my automation in Logic Apps. Is there a way to import this into Azure? When I go to solutions, I don’t see an import option. Thank you for your help.

    1. Hi Jeff,
      I will write in the future on how to import solutions, but in this case, we only have two cloud flows, so we cannot import them directly into a solution.
      I created a solution that contains both flows to import it. You can find the link here.
      To do it, go to solutions. Press import and then select the zip file. After that, Power Automate will display you a screen where you can add all the options, and you’ll end up with a solution with the name “Convert an HTML table into a JSON.” With this, you can call this flow a child flow in all your other Flows.
      Can you please try it and let me know if it works?
      All the best

  2. I attempted to import the 1st zip file (converting HTML to JSON) but it will not import into a PowerAutomate Flow.

      1. Hey Manuel

        I’m getting the really helpful error “Something went wrong. Please try again later”

        Is there anything you recommend trying?

        1. You may be importing this as a Template, I did that and got the same error. This needs to be imported under the Solution tab, and it’ll appear under the “My Flows”.

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon