Introduction
Yes, you read the title right, this blog is about evaluating IF condition. You might be wondering what about IF, even novice developer with no experience knows about it.
Allow me to explain a specific scenario that helps us understand it’s behavior in Logic Apps, it might blow your mind.
Some of us come from years of development experience, and at times we like to skill up ourselves to various other technologies, which leaves us with a mindset based on our past development experience and programming habits, which we gained over the years. When clients requirements are approached based on these backgrounds, we expect the code to work with the certain flow and these are where rules are broken while using IF condition in Azure Logic Apps.
Understanding JSON expression
JSON expression evaluates string to JSON object using syntax as shown below
json({"Person":{"Name": "Simpson"}}) evaluates to var name = Person.Name as Simpson
But, the same json(null), throws an error (important), avoid where possible.
Understanding IF condition
IF don’t need any special introduction, we know how it works. As we know, it has two code blocks that are evaluated based on the condition and falls to one of the blocks. As applies to Logic Apps and below is the syntax for it.
@if("condition","true","false")
To understand IF better, let’s also look into @equals(), it is a simple expression that returns true or false based on the given input and provided comparing value.
Example 1
Below is just an example, please ignore simple equality condition.
@if(equals(1,1),"true1","false1")
Result: true1
Example 2
@if(equals(1,2),"true1","false1")
Result: false1
Now, let us take our person JSON and understand it.
@if(equals(1,1),"Merge",json({"Person":{"Name": "Homer"}}) ['Name'])
Result: Merge
and similarly when the comparison is not equal
@if(equals(1,2),"Merge",json({"Person":{"Name": "Homer"}}) ['Name'])
Result: Homer
Now, recall that IF falls to one of the code blocks and returns. But in case of Azure Logic Apps, it evaluates both the code blocks and returns one code block result, that it falls into.
Here is the proof
For Example, if I do something like below, it should result as “Merge”, but it actually throws an error. According to current Logic Apps, this is the current behavior.
@if(equals(1,1),"Merge",json(null) ['Name'])
Result: error
And similarly when not equal
@if(equals(1,2),"Merge",json(null) ['Name'])
Result: error
The above examples imply that Logic App evaluates both the code blocks and returns one.
Actual error thrown is as below from real logic app
InvalidTemplate. Unable to process template language expressions in action ‘Compose’ inputs at line ‘1’ and column ‘1525’: ‘The template language function ‘json’ expects its parameter to be a string or an XML. The provided value is of type ‘Null’. Please see https://aka.ms/logicexpressions#json for usage details.’.
I got same error, Do you have any update?
Beto, apparently that is how it works. It is going evaluate both sides of if-condition, make sure not to use if-condition if you know it may be null
correct