web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Condition if Receive O...
Power Apps
Unanswered

Condition if Receive Object or An Array

(1) ShareShare
ReportReport
Posted on by

Hi 

 

Is there a function to check if variable contains Object or Array?

Like if Array do this ; if object do this.

 

This is to handle JSON Schema errors

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @u4jaanus 

    There really isn't anything like that.  Everything in PowerApps is stored as a JSON object, so there is no distinction between them.

    But more importantly, you cannot change the type of a variable in your PowerApp.  This will cause issues during design and if you work around that, will most likely fail during Play time.

    PowerApps always infers the schema for anything in a variable.  If you attempt to alter that elsewhere in the app, the designer will complain about it and in many cases get into a inference deadlock (variable is considered invalid everywhere in the app and the only recourse is to exit the app designer and go back in to correct it).

     

    I hope this is helpful for you.

  • u4jaanus Profile Picture
    on at

    Thanks for replying, 
    Sorry I think I was not clear enough in my question.

    What I wanted to ask; if there is a way to handle an object and array sent from power app.. when we set flow to Ask is PowerApp and once data is passed to power app. can we check if data is an object or array so we could handle the request accordingly.

     

  • HEATFreight Profile Picture
    1,024 on at

    I have the same issue, except I need to check the response body of an HTTP request which can sometimes return an object and other times return an array.

    How can I test which is the case? I need a condition to check if outputs('HTTP_Request')?['body'] is an object or if it's an array then I need to use outputs('HTTP_Request')?['body'][0] because my schema is looking for an object, not an array.

    I tried "type": ["object","array"] but couldn't figure out how to deal with "items" vs "properties" in the schema. I tried including both the "items":{"type":"object","properties":{...}} from the array version and the "type":"object","properties":{...} from the object version together in the same schema like this:

    "Output": {
     "type": ["object","array"],
     "properties": {...},
     "items": {
     "type": "object",
     "properties": {...}
     }
    }

     
    But that gave me an error when adding the Power Automate flow back to the Power App which calls it.

    So I basically just need some equivalent to @{Coalesce(outputs('HTTP_Request')?['body'][0],outputs('HTTP_Request')?['body'])} u, which does not work because

    property '0' cannot be selected. Object properties can only be selected by names. Please see https://aka.ms/logicexpressions for usage details.'.


    How can I coalesce the two possibilities of a response body which can be either an array or an object?

  • HEATFreight Profile Picture
    1,024 on at

    Ok, this is how I solved it, although I would prefer to make this work as a simple function...

    I used Scope actions to run the logic for both scenarios, then coalesce the Compose outputs from each Scope. The outputs('HTTP_Request')?['body'] is always valid, but outputs('HTTP_Request')?['body'][0] is invalid when the output body is an object. So, you need a "run after condition" of "successful/failed/skipped/timed out" for the array Scope action, but only need "run after successful" for the object Scope action. You put the array version of the output compose first in the coalesce function, followed by the object version of the output compose action. Thus, you will get a valid result either way. I feel like I've solved this problem before using a simpler method, but I couldn't figure it out this time around. Would appreciate any advice about how to do this in functions only.

    HEATFreight_0-1643752840510.png

     

  • SriniV Profile Picture
    16 on at

    I used array() function and it helped. If an object is passed as an input, it returned an array with 1 entry and if an array is passed as an input it returned the same array.

     

    I also tried type object and array in the same key and it worked as well. 

  • amunra Profile Picture
    9 on at

    Thanks SriniV,

     

    your comment is way underrated! Your solution solved our question if the element is a string or an array because our XML > JSON flow made all our items with only one element a string and all others an array (obvious).

  • OlivePesto Profile Picture
    9 on at

    Based on SriniV's post above, I was able to build an expression around it using the array(). As per SriniV's suggestion, it does return an array if it's already an array (so not a double or nested array), so the below step is just an extra measurement just in case it stops working.

     

    This is the final expression:

     

    if(startsWith(string(outputs('your_Function')?['body']), '['), outputs('your_Function')?['body'], array(outputs('your_Function')?['body']))

     

     

    If you have an input for example from an Azure function then you'd need to point out to the Azure Function's body.

     

    outputs('your_Function')?['body']

     


    Before you start checking if it has a left square bracket "[" in order to determine if it's an array, you first need to convert it into a string because the startsWith expression can check only strings.

     

    string(outputs('your_Function')?['body'])

     


    Now you can wrap your string around a startsWith so you can check if it starts with a square bracket '['. This will return a true or false.

    startsWith(string(outputs('your_Function')?['body']), '[')


    Since you can get a true or false, now's the time to wrap it with an if expression. If it comes true as an array, then the returned output of the compose will be just the body of the function. If it's false, then the array expression will convert the output of the function into an array.

    if(startsWith(string(outputs('your_Function')?['body']), '['), outputs('your_Function')?['body'], array(outputs('your_Function')?['body']))

     

    Now that you have a consistent array output, you can parse the output using a single schema. This way it saves you from building things twice depending on whether you get a single object as a result or an array.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 834

#2
Valantis Profile Picture

Valantis 533

#3
Haque Profile Picture

Haque 410

Last 30 days Overall leaderboard