cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Vstar19
Helper IV
Helper IV

Looping through a string.

Hello. 

 

I am using Power Automate to pull in all some records. I am using the HTTP Action to pull in service now records, which works and it outputs my results as below

json.PNG

 

Now i need to loop through each result and return some data. Inside each json result, there is a description, the description contains some information which i need to use to do a condition. The description contains in the following format 

FirstName : John, 

Last Name : Smith, 

ID Number : 1234, 

 

I have tried to do a For Each loop, but i get the following error : 

"The execution of template action 'Apply_to_each' failed: the result of the evaluation of 'foreach' expression '@body('HTTP')' is of type 'Object'. The result must be a valid array."

 

Which actually makes sense, i cant loop through a string. 

 

Can someone help me achieve my goal here? Which is loop through each item and return the description. 

Then i want to put a condition to say if the description to say if ID = 12, then do this. 

 

Thanks in advance. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Paulie78
Super User III
Super User III

I'm guessing you want body('HTTP')['result'] and loop through that instead. But if you could post the JSON sample (the actual JSON). Then one of us can tell you in a snap.

View solution in original post

10 REPLIES 10
DamoBird365
Community Champion
Community Champion

Hi @Vstar19 

 

I believe you need to use something like item()?['FirstName'] or in your case item()?['ID Number'].

 

To test you are getting an output in your apply to each you can temporarily include a compose action and then check back on the history.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Cheers,
Damien


P.S. take a look at my new blog here

Vstar19
Helper IV
Helper IV

Where do i use that expression.? 

Essentially, i want to loop from a json file. 

 

I tried to use that expression in the for each , but i get an error the same error as above about it not being a valid array. 

Paulie78
Super User III
Super User III

Can you post a sample of the JSON output from the HTTP Request? If you are using the "Body" then it will contain other data besides your json array most likely.

Here is a sample of the data. 

sampledata.PNG

There are several records, as you can see just under result, there is a 0, then the next record is 1,2 and so forth. 

 

My aim is to a for each where by i can loo through each results (0,1,2,3,4), extract some data from a column called 'Description'. (This is not shown on the screenshot), but essentially, the description column contains a number i need. 

I then would like to get that number from the description filed and use it as a condition to do a check aganist another number i have. 

 

When i try to put the body of the json in a ForEach i get the error Apply_to_each' failed: the result of the evaluation of 'foreach' expression '@body('HTTP')' is of type 'Object'. The result must be a valid array.

 

This is where i am getting stuck. 

 

Thanks. 

 

 

 

can anyone assist?

DamoBird365
Community Champion
Community Champion

Sorry @Vstar19 

 

I had missed this, remind me where you are at.  Can you post the actual JSON??

 

Damien

i am still unable to loop through each item from the JSON, i get the error "Apply_to_each' failed: the result of the evaluation of 'foreach' expression '@body('HTTP')' is of type 'Object'. The result must be a valid array"

 

I want to loop through each item and return the description. 

 

Thanks

DamoBird365
Community Champion
Community Champion

@Vstar19 you could try create an array with the createarray() expression and then use your original expression?

 

It's hard to attempt the same with a screenshot, would need a copy of your array to be 100%

 

Damien

Paulie78
Super User III
Super User III

I'm guessing you want body('HTTP')['result'] and loop through that instead. But if you could post the JSON sample (the actual JSON). Then one of us can tell you in a snap.

View solution in original post

Vstar19
Helper IV
Helper IV

@Paulie78 Thank you.

 

It was  such a silly mistake i was making. I was trying to loop through a body('HTTP')?['result']?[0]?['number'] and it kept failing. 

 

The solution was body('http')?['result'] as you said. 

 

Such a novice mistake.

 

Thank you both for your help. 

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (2,262)