cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Super User
Super User

Transforming an Array into another Array

This is tricky. I'm unsure how to tackle the problem. 

What I need is a dynamic array based on the number of user input items in the Button trigger.

Example:

  • There are multiple Text inputs on a Button trigger
  • User selects an action to perform ("text" below)
  • User enters a List Name ("text_1" below)
  • User enters from 1-5 fieldNames ("text_2", _4, _6, _8 or _10" below) and
  • 1-5 corresponding fieldTypes ("text_3, _5, _7, _9 or _11" below)
  • Resulting in the following triggerBody()...

 

[
  {
    "text": "Create New List",
    "text_1": "my cool list",
    "boolean": true,
    "text_2": "test1",
    "text_3": "DateTime",
    "text_4": "test2",
    "text_5": "Number",
    "text_6": "test3",
    "text_7": "Multi-Line Text",
    "text_8": "test4",
    "text_9": "Person",
    "text_10": "test5",
    "text_11": "Single Line Text"
  }
]

...if the user entered all fields.

 

They may only enter 1 fieldName and 1 fieldType or...

[
    "text_2": "test1",
    "text_3": "DateTime"
]

...they may enter two fieldNames and fieldTypes OR all 5.

 

[
    "text_2": "test1",
    "text_3": "DateTime",
    "text_4": "test2",
    "text_5": "Number"
]

How do I iterate over the user-entered array to produce a dynamic array in the following format?...

[
  {
    "fieldName": "test1",
    "fieldType": "DateTime"
  }
]

I'm certain it will be a Compose Action within an Apply to Each Action, but I don't quite get how to parse the array. 

Any ideas?

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Dual Super User
Dual Super User

Re: Transforming an Array into another Array

@ericonline 

 

 

https://github.com/rdorrani/Microsoft-Flow/blob/master/TransforminganArrayintoanotherArray_201910081...

 

 

--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

View solution in original post

6 REPLIES 6
Dual Super User
Dual Super User

Re: Transforming an Array into another Array

@ericonline 

 

 

https://github.com/rdorrani/Microsoft-Flow/blob/master/TransforminganArrayintoanotherArray_201910081...

 

 

--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

View solution in original post

Super User
Super User

Re: Transforming an Array into another Array

Thank you for these gems @RezaDorrani ! Its been fun and fruitful to dissect your expressions!

I feel like the solution is almost there. 

1. How do I filter out the three lines shown below before manipulating the rest?

split(outputs('input_into_string'),','):

[
  "{\"text\":\"Create New List\"",                  //HOW DO I FILTER OUT THIS LINE
  "\"text_1\":\"powerappers cool list\"",           //AND THIS LINE
  "\"boolean\":true",                               //AND THIS LINE? THEY ARE NOT APPLICABLE TO THE ARRAY
  "\"text_2\":\"testField1\"",
  "\"text_3\":\"Person\"",
  "\"text_4\":\"testField2\"",
  "\"text_5\":\"Single Line Text\"",
  "\"text_6\":\"testField3\"",
  "\"text_7\":\"Multi-Line Text\"",
  "\"text_8\":\"testField4\"",
  "\"text_9\":\"DateTime\"",
  "\"text_10\":\"testField5\"",
  "\"text_11\":\"Boolean\"}"
]

Also inside the For Each, where the final array is built, you're using...

{ 
  "fieldName": @{replace(split(item(),':')?[1],'"','')}, 
  "fieldType": @{replace(split(outputs('Compose_2')?[variables('varCounter')],':')?[1],'"','')} 
}

...resulting in...

[
  {
    "fieldName": "Create New List",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "powerappers cool list",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "true",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "testField1",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "Person",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "testField2",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "Single Line Text",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "testField3",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "Multi-Line Text",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "testField4",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "DateTime",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "testField5",
    "fieldType": "powerappers cool list"
  },
  {
    "fieldName": "Boolean}",
    "fieldType": "powerappers cool list"
  }
]

2. How do I get the final array to be...

[
  {
    "fieldName": "testField1",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField2",
    "fieldType": "Single Line Text",
  },
  {
    "fieldName": "testField3",
    "fieldType": "Multi-Line Text",
  },
  {
    "fieldName": "testField4",
    "fieldType": "DateTime"
  },
  {
    "fieldName": "testField5",
    "fieldType": "Boolean"
  }
]

Definitely buying you lunch next time you're in Seattle :)!

Dual Super User
Dual Super User

Re: Transforming an Array into another Array

@ericonline 

 

Very easy

In the flow that I provided in the Compose 3 action where I split into an array

 

just use

 

skip( split(outputs('Compose_2'),','),3)
 
This step will skip the first 3 items of the array
 
Seattle lunch is confirmed 🙂
--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.
 
 
Super User
Super User

Re: Transforming an Array into another Array

Wow, you're right, that skip function was very easy. 

Moving to the last hurdle. The array is now created correctly...

[
  "\"text_2\":\"testField1\"",
  "\"text_3\":\"Person\"",
  "\"text_4\":\"testField2\"",
  "\"text_5\":\"Single Line Text\"",
  "\"text_6\":\"testField3\"",
  "\"text_7\":\"Multi-Line Text\"",
  "\"text_8\":\"testField4\"",
  "\"text_9\":\"DateTime\"",
  "\"text_10\":\"testField5\"",
  "\"text_11\":\"Boolean\"}"
]

...the final split is resulting in...

[
  {
    "fieldName": "testField1",
    "fieldType": "Person"
  },
  {
    "fieldName": "Person",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField2",
    "fieldType": "Person"
  },
  {
    "fieldName": "Single Line Text",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField3",
    "fieldType": "Person"
  },
  {
    "fieldName": "Multi-Line Text",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField4",
    "fieldType": "Person"
  },
  {
    "fieldName": "DateTime",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField5",
    "fieldType": "Person"
  },
  {
    "fieldName": "Boolean}",
    "fieldType": "Person"
  }
]

...intead of...

[
  {
    "fieldName": "testField1",
    "fieldType": "Person"
  },
  {
    "fieldName": "testField2",
    "fieldType": "Single Line Text"
  },
  {
    "fieldName": "testField3",
    "fieldType": "Multi-Line Text"
  },
  {
    "fieldName": "testField4",
    "fieldType": "DateTime"
  },
  {
    "fieldName": "testField5",
    "fieldType": "Boolean"
  }
]
Dual Super User
Dual Super User

Re: Transforming an Array into another Array

@ericonline 

 

You will need to ensure ur inputs are in order

 

for example

[
  {
    "text": "Create New List",
    "text_1": "my cool list",
    "boolean": true,
    "text_2": "test1",
    "text_3": "DateTime",
    "text_4": "test2",
    "text_5": "Number",
    "text_6": "test3",
    "text_7": "Multi-Line Text",
    "text_8": "test4",
    "text_9": "Person",
    "text_10": "test5",
    "text_11": "Single Line Text"
  }
]

skip the top 3 will give

 

[
  {
    "text_2": "test1",
    "text_3": "DateTime",
    "text_4": "test2",
    "text_5": "Number",
    "text_6": "test3",
    "text_7": "Multi-Line Text",
    "text_8": "test4",
    "text_9": "Person",
    "text_10": "test5",
    "text_11": "Single Line Text"
  }
]

 

The code will create array of objects in batches of 2 so the remaining number of items must be even

 

Super User
Super User

Re: Transforming an Array into another Array

Ha! Got it! Thank you @RezaDorrani . 

I had a Set Variable action (named "Increment variable") instead of an Increment Variable action inside the For Each!

Thanks so much!

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 205 members 6,269 guests
Please welcome our newest community members: