Extract column names from dynamic JSON schema

Extract column names from dynamic JSON schema | Power Automate

Tabla de Contenidos

There are some scenarios where JSON results returned from a service will have different column names or attributes. For example this JSON sample has 6 columns – Amount, Description, Product Code, Quantity and Unit Price:

				
					[
   {
      "Amount":{
         "type":"currency",
         "content":"$65.00"
      },
      "Description":{
         "type":"string",
         "valueString":"SiteReadyB",
         "content":"SiteReadyBC"
      },
      "ProductCode":{
         "type":"string",
         "valueString":"SiteReadyBC",
         "content":"SiteReadyBC"
      },
      "Quantity":{
         "type":"number",
         "valueNumber":1,
         "content":"1"
      },
      "UnitPrice":{
         "type":"currency",
         "valueCurrency":{
            "currencySymbol":"$",
            "amount":65,
            "currencyCode":"CAD"
         }
      }
   }
]

				
			

But sometimes, more columns – for which we don’t know the name of – could present in the results or not all columns will be available. This blog will show the steps required in Power Automate to extract all the column names present in a JSON result.

Step 1. Create an Array variable

Array variable Column

Step 2. Use xpath and xml functions

Our JSON is for now being stored in an action called “Select” in Power Automate Platform

Convierte tus ideas en soluciones digitales

Nuestro equipo te guía paso a paso para crear aplicaciones personalizadas en Power Platform.

Use xpath Column

Add a compose action, and use the following formula:

				
					@{xpath(xml(json(concat('{ "root": { "columns": ', first(body('Select')), ' } }'))), '/root/columns/*')
				
			

What this formula is doing is getting the first line of the JSON results and concatenating the attributes “root” and “columns” to it. Behind the scenes it should look something like this:

				
					{ "root": { "columns": ', {your-json-record}, ' }
				
			

Then, it converts this into XML and finally it extracts the root/columns attributes from it.

Step 3. Obtain column names

Add an “Apply to each” action and map the input to the results of the compose action. Inside the loop, add an “append to array variable” action.

xpath Column

Inside the “append” action, add the following code:

				
					replace(first(split(string(items('For_each_column_in_details')),'>')),'<','')
				
			

What this will do is extract the column name from the XML format:

Compose 2 Column

We’re obtaining the text before the first “>” character and then just removing the first “<”.

Step 4. Print the results

Add a Compose action and assign to it the value of the array that contains the column names.

¿Está su negocio preparado para la automatización?

Automatiza procesos con Microsoft Power Platform.

ar_line Column

After the “For each column in details” loop finished the results should look like this:

Outputs Column

As a trusted Power Platform Consultancy, we’re equipped to guide your business through this digital transformation. Our Power Platform consulting services are designed to enhance your processes, ensuring that you make the most out of Microsoft’s tools. Let us help you streamline operations efficiently and effectively.

Power Automate has amazing functions that can be used to work many formats of JSON results, combining Arrays, Loops and Compose actions along with XML functions is the perfect mix to extract dynamic column names. Contáctanos if you have any questions or if you’d like some help on Power Automate or PowerFx!

Autor
Consultor de Power Platform | Experto en Automatización de Procesos de Negocio
Consultor certificado de Microsoft Power Platform y Arquitecto de Soluciones con más de 4 años de experiencia aprovechando Power Platform, Microsoft 365 y Azure para descubrir continuamente oportunidades de automatización y reimaginar procesos.