cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
grovercorner2
Level: Powered On

Flow - expression to test if item exists in JSON

I have a JSON coming in with a variable number of items in it. I have managed to parse the json just fine.

 

However, I would like setup a condition in flow:

if item 'user_id' exists in the json, do this..., else do this.

 

How do I test for "does 'user_id' exist"? All the expressions I have tried give me an error bacause the content for 'user_id' doesn't exist, so how can I run an expression against it. The flow errors out.

Thanks in advance.

4 REPLIES 4
Community Support Team
Community Support Team

Re: Flow - expression to test if item exists in JSON

Hi @grovercorner2,

 

Could you provide an example of JSON data? A set of sample data so we have room for testing.

If possible, you could treat it as a String and use contains to determine if it contains the expected value.

Or use Parse JSON action to process it and get the properties contained in it.

Specifically, look at the structure of JSON data, please provide more details.

 

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
mboonie
Level 8

Re: Flow - expression to test if item exists in JSON

I have a similar situation, although mine involves checking whether an array is present in the data (not whether the array is empty... semantics!)

I have a JSON data dump from a "Teams" database. If a team is actually populated with any member info at all, there is an array in the file. Here is a drastically truncated example:

[
    {
        "projectID":"ea764a86-6c2d-487d-8a70-8b8ec32aae5b",
        "teamMembers":[
            {
                "userID":"",
                "subRole":"BD",
                "roleName":"JBIO Team Member",
                "fullName":"TBD"
            },
            {
                "userID":"",
                "subRole":"BDS",
                "roleName":"JBIO Team Member",
                "fullName":"TBD"
            }
        ],
        "tmProfile":{
            "goal":"(my team goal)"
        }
    }
]

 

However, If there is no member information at all, the JSON is delivered without any array, e.g.:

[
    {
        "projectID":"ea764a86-6c2d-487d-8a70-8b8ec32aae5b",
        "tmProfile":{
            "goal":"(my team goal)"
        }
    }
]

How best to check whether the array "teamMembers" (or any node or sub-schema, for that matter) even exists? Do I just have to set up my "Run After" to ignore an error thrown by my attempt at a "Apply to each"?

 

Thanks!

 

grovercorner2
Level: Powered On

Re: Flow - expression to test if item exists in JSON

I gave the 'contains' a try, wasn't successful at first, but then I found another field in the json that I could use as a conditional for if the other field was present or not.

 

I didn't try 'contains' very hard, I suspect I made a mistake. I think it should work.

digiservice
Level: Powered On

Re: Flow - expression to test if item exists in JSON

This works for me in a ForAll:

contains(variables('jsoncollectionname'),items('Apply_to_each')['itemtocheck'])

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: 146 members 3,077 guests
Recent signins:
Please welcome our newest community members: