Extracting Keys from a JSON in Power Automate


Extracting Keys from a JSON in Power Automate


Introduction

Power Automate is a powerful tool that allows you to automate workflows and processes. In this blog, we'll explore a specific flow designed to extract keys from a JSON and return an array of those keys. Let's dive into the details of each step and understand how this flow works.

Flow Overview

The flow, named "Extract Keys from a JSON," is triggered manually using a button. When the flow is triggered, it takes a JSON input and extracts its keys into an array. The array is then returned as a response.

Step 1: Trigger

The flow starts with a manual trigger. A button is created to initiate the flow manually. This trigger is set up to accept a JSON input.


Step 2: Compose

After the trigger, the first action is the "Compose" action. Its purpose is to convert the input JSON into a usable format for further processing. The formula used in this action is:

Formula:

json(triggerBody()['text'])


This formula uses the json function to parse the JSON text from the trigger.

Step 3: Initialize Variable

Next, a variable named "count" is initialized with a value of 0. This variable will be used to iterate through the array of keys.


Step 4: Compose (2)

This action is used to convert the output of the previous "Compose" action into JSON format. The formula is:

Formula:

json(string(outputs('Compose')))


Step 5: Compose (3)

In this step, the output from the previous Compose action is further processed using the @xml function. The formula is:

Formula:

xml(outputs('Compose_3'))


Step 6: Compose (4)

This action uses the @xpath function to extract specific elements from the XML output. The formula is:

Formula:

xpath(outputs('Compose_4'), '/root/*')


Step 7: Do Until

A "Do Until" loop is initiated to iterate through the extracted XML elements until the end is reached. The loop condition is:

Formula:

equals(length(outputs('Compose_6')), variables('count'))


The loop will continue until the length of the extracted elements matches the current count.


Step 8: Compose (5)

Within the loop, another "Compose" action is used to extract the name of each element. The formula is:

Formula:

xpath(outputs('Compose_6')[variables('count')], 'name(/*)')


Step 9: Append to Array Variable

The name of the element is then added to an array variable named "varArray."


Step 10: Increment Variable

After extracting the name of an element, the "Increment Variable" action is used to increment the count by 1.


Step 11: Respond to a Power App or Flow

Finally, the array of keys is returned as a response to the Power App or Flow that initiated the process. This is achieved using the "Respond to a Power App or Flow" action. The schema includes a property named "array," which contains the array of keys.

Response:

{ "array": "@outputs('Compose_5')" }


Conclusion

In conclusion, this Power Automate flow effectively extracts keys from a JSON input, demonstrating the capability of Power Automate in handling JSON data. By breaking down each step and understanding the formulas used, users can gain insights into the workflow and customize it to meet specific requirements.

Power Automate remains a versatile and robust tool for automating tasks and workflows. This particular flow showcases how it can be employed to handle JSON data efficiently, making complex processes more manageable and streamlined.

As you explore and implement this flow, don't hesitate to experiment with variations and extensions to adapt it to your

 

Comments

  1. Can you share the business case where you implement it?

    ReplyDelete
  2. Where is compose 6?

    ReplyDelete

Post a Comment