cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
grovercorner2
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.

5 REPLIES 5
v-bacao-msft
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.
mboonie
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!

 

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
Advocate II
Advocate II

This works for me in a ForAll:

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

DayAfter_0-1616073708880.png

 

Similar to @digiservice, I have found this same approach successful:

 

 

if (true === contains(string(variables('arrYourArrayOfJSON')),items('Apply_to_each')?['itemtocheck']))

 

 

In a scenario I had once in an 'Apply to each', if an item wasn't found I could append it to the JSON array I was building, else in the 'If yes' path I would do nothing.

 

Note:  The item to check would need to be an independent value and such that it wouldn't be contained as a part of another value, which could throw off your results.

Helpful resources

Announcements
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

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Users online (46,658)