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

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 III
Helper III

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 III
Helper III

@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
MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Users online (52,670)