cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate I
Advocate I

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
Highlighted
Community Support
Community Support

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.
Highlighted
Resolver I
Resolver I

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!

 

Highlighted

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.

Highlighted
Advocate I
Advocate I

This works for me in a ForAll:

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

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Top Solution Authors
Top Kudoed Authors
Users online (8,783)