cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SCTdan
Level 8

How to handle empty JSON response

At the bottom of the screenshot, I have a for each loop (which was automatically created) based on the 'customer' output of a JSON response.  That lookup pulls a customer ID based on their email domain.  The issue I'm having is what happens when no customer is found, and I get a blank / null response (see body output on right).  Because the for each loop is predicated on the customer array having something in it, it just gets skipped.

 

Ideally I just want it to assign a static ID if it doesn't return a customer, but with that for each based on the customer array having something, I'm not sure what the best solution is here.

 

There will only ever be one customer returned, so I don't think the for each is necessary is it?  Seems like I'm stuck though because I can't move the actions out of there.  Is there a better solution than just recreating those steps without a for each loop?

 

Cmr1xzJ

6 REPLIES 6
Community Support Team
Community Support Team

Re: How to handle empty JSON response

Hi @SCTdan,

 

I afraid that you couldn'y move the for each even though only one customer ever be returned.

 

Best regards,

Alice

JoshL
Level: Powered On

Re: How to handle empty JSON response

Firstly, you cannot move or delete actions that are currently using or are currently being used by other actions.Work from the bottom and remove the input to the action before trying to move or delete it. Next, you could have a conditional after you pull in the JSON to check the customer information then handle it accordingly. 

 

 

@not(empty(items('Apply_to_each')?['Customers']))

OR

@not(empty(items('Apply_to_each')?['CustomerID']))

 

If the customerID is not empty then go ahead and do what you want. If it is empty then you can populate it yourself with something else. The possibilities are endless. I did a similar thing for a date in my flow. 

 

121.JPG

 

Smiley Happy

 

 

SCTdan
Level 8

Re: How to handle empty JSON response

Can you show me what yours looks like above that condition?  I can't figure out where to actually put that condition, everywhere I try I get this error:  "The template validation failed: 'The inputs of template action 'Condition_3' at line '1 and column '14342' is invalid. Action 'Apply_to_each' must be a parent 'foreach' scope of action 'Condition_3' to be referenced by 'repeatItems' or 'items' functions.'."

 

It'll only let me put that in the actual for each loop, which isn't helpful because that gets skipped if there's no data in customers.

 

Thanks

Highlighted
JoshL
Level: Powered On

Re: How to handle empty JSON response

What that is saying is that your template for your JSON parsing is incorrect. You may want to regenerate the template. It also says that for your "Condition_3" you're trying to refer to an apply_to_each that the condition isn't inside of. The reason why it allows you to for it in the loop is because the expression is explicity referencing it. 

 

What I have is a little complicated so there wouldn't be any use in showing you. This might be the solution you're looking for:

 

1213.JPG

 

Once you have your data. Check it. If it's empty do something with it and if it isn't ditto. 

 

SCTdan
Level 8

Re: How to handle empty JSON response

Gotcha.  I was having issues with null values, so I modified the JSON template to accept null values in some cases.  Guessing I need to re-think that part.

 

Thanks for the input

SCTdan
Level 8

Re: How to handle empty JSON response

Figured out a workaround that didn't require me to look at JSON this early in the morning.  I initialized a variable with what I want the fallback ID to be.  Then I used the for each to set that variable to the customer ID if anything was returned.  So if there's nothing returned, that gets skipped, but the variable has it's original fallback ID so it continues to run.  It appears to work so far...

 

  yOunn7J

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020.

thirdimage

Flow Community User Group Member Badge

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

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 Flow Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 368 members 3,912 guests
Please welcome our newest community members: