cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Nithin_Vanam
Helper II
Helper II

Looping through an Array

Hello,

 

I am trying to loop through an Array which has key, value pairs that is something like this:

 

[
  {
    "Q1": "65a4bea6-ef6e-ea11-a811-000d3aa3ee47"
  },
  {
    "Q5": "5b564b8a-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q11": "c97a7c94-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q12": "33fcc49e-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q13": ""
  },
  {
    "Q14": ""
  }
]

 

 

However how do i retrieve the value for a specific key say for Q1 or Q2 etc...?

I tried something like this with functions to reflect the function within a compose action:  variables('QuestionReferenceArray')[0]['Q1']

 

variables('QuestionReferenceArray')[int(variables('Question Reference Index'))][outputs('Compose_14')]

 

 

This is the error i get:

 

InvalidTemplate. Unable to process template language expressions in action 'Compose_12' inputs at line '1' and column '23867': 'The template language expression 'variables('QuestionReferenceArray')[int(variables('Question Reference Index'))][outputs('Compose_14')]' cannot be evaluated because property ''Q1'' doesn't exist, available properties are 'Q1'. Please see https://aka.ms/logicexpressions for usage details.'.

 

 

Not sure if i am doing this right but would be great if someone could guide me how i could read array data based on a key. 

My flow run where the error occurs:

Array Flow 1.PNG

 

Flow Actions:

Array Flow 2.PNG

Thanks in advance for the help.

1 ACCEPTED SOLUTION

Accepted Solutions
v-litu-msft
Community Support
Community Support

Hi @Nithin_Vanam,

 

The Index in the array is not correct, the index of array starts from 0, the index of the first member should be 0, so you should sub 1 when you input expression, for example:

variables('QuestionReferenceArray')[add(int(variables('Question Reference Index')),-1)][outputs('Compose_14')]

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Jcook
MVP

Hello @Nithin_Vanam 

 

Please elaborate in what you are trying to achieve. From what I can gather would this work:

 

I took your example from above and added it to a Compose action.

[
  {
    "Q1": "65a4bea6-ef6e-ea11-a811-000d3aa3ee47"
  },
  {
    "Q5": "5b564b8a-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q11": "c97a7c94-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q12": "33fcc49e-a17c-ea11-a811-000d3aa3ee47"
  },
  {
    "Q13": ""
  },
  {
    "Q14": ""
  }
]

 

Now I added a Parse JSON action, and added the Array as the content.

See below for the Schema I usedSee below for the Schema I used

 Next I added a Apply to each loop and set the input as the Body from the Parse JSON action

 This will iterate through each item.

 

If you want to just get one value, there is no need for the loop as all data can be referenced individually thanks to the Parse JSON action

 

The schema that was used in the Parse JSON action:

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "Q1": {
                "type": "string"
            },
            "Q5": {
                "type": "string"
            },
            "Q11": {
                "type": "string"
            },
            "Q12": {
                "type": "string"
            },
            "Q13": {
                "type": "string"
            },
            "Q14": {
                "type": "string"
            }
        },
        "required": []
    }
}

Did I answer your question? Mark my post as a solution!

If you like my post please hit the Thumbs Up


Proud to be a Flownaut!


Check out my blog for Power Automate tips,
tricks, and guides
FlowAltDelete





v-litu-msft
Community Support
Community Support

Hi @Nithin_Vanam,

 

The Index in the array is not correct, the index of array starts from 0, the index of the first member should be 0, so you should sub 1 when you input expression, for example:

variables('QuestionReferenceArray')[add(int(variables('Question Reference Index')),-1)][outputs('Compose_14')]

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

dperez13
Post Patron
Post Patron

DOES JSON scheme have to be the same length as the input? what if sometimes you have less columns of data, youre json schema wont match whats passed into it

@dperez13 

Thats fine, as long as they are not set as Required in the schema.

 

You can technically just use this as your schema, but you wont be able to use dynamic content within your flow.

{}

 

 


Did I answer your question? Mark my post as a solution!

If you like my post please hit the Thumbs Up


Proud to be a Flownaut!


Check out my blog for Power Automate tips,
tricks, and guides
FlowAltDelete





Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (5,030)