cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dht6000
New Member

Detect if variable is an array or a string

Hi,

 

I'm working on a Flow that fetches JSON from an API. the schema has a couple of properties where the returned data can be a string, an array of strings or null (missing entirely).

 

The snippet of the schema looks like this

"name": { "anyOf" : [
{
"type": "string"
},
{
"type": "array"
},
{
"type": "null"
}
]
},

....

 

I'd like to be able to have the data in a single string for onwards use, and have used the join function when an array is returned OK, but it fails when a string is returned.

 

Is it possible to tell what the returned type of a property is so I can select the right method to handle it? I've read around it quite a bit but am stuck.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
leo85
Super User
Super User

Hi @dht6000 ,

As far as I know, there is no easy out-of-the-box way to differentiate between an array or string.

 

You could use the following work around:

1. Initialize a string variable for the result, that saves the result of the API request.

1. Create a scope action.

2. Within the scope, set the string variable, assuming that you will receive an array. Note: you cannot initialize the variable within the scope.

3. After the scope, initialize a second string variable, when the scope fails (run-after). This variable saves the result of the scope.

 

Now you have two options:

1. The scope succeeds and you've got the result in your first string variable.

2. The scope fails. It's likely, your result was not an array, but a string or null. You can check with the result in the string variable.

 

leo85_1-1640866375263.png

 

The result if the scope, because it was given a string, looks like this:

Unable to process template language expressions in action 'Set_variable' inputs at line '1' and column '9893': 'The template language function 'join' expects its first parameter to be an array. The provided value is of type 'String'. Please see https://aka.ms/logicexpressions#join for usage details.'

I hoped this helps.

 

Regards,

Leo

 

----------------------------------------------

If my answer solved your issue, please mark it as complete.

If what I wrote helped you, please give it a thumbs up.

View solution in original post

2 REPLIES 2
leo85
Super User
Super User

Hi @dht6000 ,

As far as I know, there is no easy out-of-the-box way to differentiate between an array or string.

 

You could use the following work around:

1. Initialize a string variable for the result, that saves the result of the API request.

1. Create a scope action.

2. Within the scope, set the string variable, assuming that you will receive an array. Note: you cannot initialize the variable within the scope.

3. After the scope, initialize a second string variable, when the scope fails (run-after). This variable saves the result of the scope.

 

Now you have two options:

1. The scope succeeds and you've got the result in your first string variable.

2. The scope fails. It's likely, your result was not an array, but a string or null. You can check with the result in the string variable.

 

leo85_1-1640866375263.png

 

The result if the scope, because it was given a string, looks like this:

Unable to process template language expressions in action 'Set_variable' inputs at line '1' and column '9893': 'The template language function 'join' expects its first parameter to be an array. The provided value is of type 'String'. Please see https://aka.ms/logicexpressions#join for usage details.'

I hoped this helps.

 

Regards,

Leo

 

----------------------------------------------

If my answer solved your issue, please mark it as complete.

If what I wrote helped you, please give it a thumbs up.

Thanks @leo85 that worked a treat. I ended up not using the second string but feeding the Scope output straight to a Condition to check for Failed or Aborted in the status, if that is true then I just set my String variable equal to the JSON output.

 

dht6000_0-1640939021865.png

 

I found that in this article https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-control-flow-run-steps-group-scopes

 

Thanks again!

 

Dave

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (2,440)