web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Select Dynamically key...
Power Automate
Unanswered

Select Dynamically keys from JSON

(0) ShareShare
ReportReport
Posted on by
Hello,
 
I have an array that contains the keys/columns that I want in my final output that looks like this:
 
[
   "a",
   "b",
   "c",
   "d"
]

Note that this array is dynamic, and can be different each run of the flow. So It can present only 3, 4 or more keys.

Then I have the following

[
{
"a": "Jonh",
"b": "2023-06-01",
"c": No,
"d": "63055571",
"e": "12",
"f": "null",
"g": "red",
"h": "Paris",
"i": "true",
"j": "55.9"
},
{
"a": "Ana",
"b": "2024-06-01",
"c": Yes,
"d": "63073455",
"e": "12",
"f": "null",
"g": "green",
"h": "London",
"i": "false",
"j": "34.5"
},
...
]

Now I need a way to be able to dynamically filter the keys/columns so I have in the second only the columns from the first array like this:

[
{
"a": "Jonh",
"b": "2023-06-01",
"c": No,
"d": "63055571"
},
{
"a": "Ana",
"b": "2024-06-01",
"c": Yes,
"d": "63073455"
},
...
]

And in the end save it to CSV. 

The issue here is that i am not finding a way to dynamically filter it to only output the columns on the first array.
Categories:
I have the same question (0)
  • Suggested answer
    Nived_Nambiar Profile Picture
    18,129 Super User 2025 Season 2 on at
     
    Try this approach which helps to dynmically filter out required keys from json array
     
     
    1. use initialize variable to define these variables
     
    2. Use apply to each to loop through the JsonArr variable as shown below
     
     
    Next steps will be inside the loop
     
    2.1 - use set variable to set tempobj variable
    2.2 - Use another loop to loop through Array1 variable 
     Inside the above loop perform the 2 steps as shown below
    2.2.1 use addProperty function to add the property to the tempobject variable as shown below using compose action
    Expression used: 
    addProperty(variables('tempobj'),items('Apply_to_each_2'),items('Apply_to_each')?[items('Apply_to_each_2')])
     
    2.2.2 use set variable to update the temp object with output from compose action 
     
    Now outside the loop and following the steps as per the inner step (first apply to each)
     
    2.3 Append the tempobject to FinalArray variable as shown below
     
     
    Now outside of all loops- use a compose action to display the Final Array variable which has required array format data
     
     
    Final flow looks like below
     
     
    Output looks like below
     
     

    Thanks & Regards,
    Nived N

    Stay connected:
    LinkedIn | YouTube | Blogs

    Was this answer helpful?
    If yes, please mark it as the solution by selecting the checkbox in the discussion thread.
    Your feedback motivates me to keep contributing. Thank you!

     
  • Chriddle Profile Picture
    8,436 Super User 2025 Season 2 on at
    My idea is to loop over the keys and add a corresponding property to the objects (which are empty at the start) in a Select:
     
     
     
     
    Select Object0:
    {
        "inputs": {
            "from": "@range(0, length(outputs('Compose_Data')))",
            "select": {}
        }
    }
    Initialze variable Result:
    {
        "inputs": {
            "variables": [
                {
                    "name": "Result",
                    "type": "array",
                    "value": "@body('Select_Objects0')"
                }
            ]
        }
    }
    Apply to each Property:
    @{outputs('Compose_Keys')}
    Select ObjectsN:
    {
        "inputs": {
            "from": "@range(0, length(outputs('Compose_Data')))",
            "select": "@addProperty(variables('Result')[item()], items('Apply_to_each_Property'), outputs('Compose_Data')[item()][items('Apply_to_each_Property')])"
        }
    }
    Set variable:
    {
        "inputs": {
            "from": "@range(0, length(outputs('Compose_Data')))",
            "select": "@addProperty(variables('Result')[item()], items('Apply_to_each_Property'), outputs('Compose_Data')[item()][items('Apply_to_each_Property')])"
        }
    }
    Compose:
    {
        "inputs": "@variables('Result')"
    }
     
     
  • Chriddle Profile Picture
    8,436 Super User 2025 Season 2 on at
    If you are not scared of using XPath and creating JSON strings, you can achieve the same result as below without loops. Just 3 Selects.
     
    With the same "Compose Keys" and "Compose Data" from below:
     
    Select
    From: @{outputs('Compose_Keys')}
     
    Map:
    split(
    	concat(
    		'"', item(), '":"',
    		join(
    			xpath(
    				xml(json(concat('{"Root":{"Item":', outputs('Compose_Data'), '}}'))),
    				concat('//Item/',item(),'/text()')
    			),
    			concat('"|*|"', item(), '":"')
    		),
    		'"'
    	),
    	'|*|'
    )
    Select 2
    From:
    range(1, length(outputs('Compose_Data')))
    Map:
    xpath(
    	xml(json(concat('{"Root":{"Item":', body('Select'), '}}'))),
    	concat('//Item/Item[', item(), ']/text()')
    )
    Select 3
    From: @{body('Select_2')}
     
    Map:
    json(
    	concat(
    		'{',
    		join(item(), ','),
    		'}'
    	)
    )
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard