How to do a 'null' check in 'if' condition action of Azure Logic App
Asked Answered
C

4

17

I've created a logic app which contains some trigger, an 'http' connector and then an 'If' condition activity. The 'http' connector returns a 'json' result say jsonObj.

I'm able to check condition as @equal(body('HTTP')['jsonObj'].someProperty,'someValue') but not able to do a null check on that someProperty value.

Below are some ways I tried which are not working.

@equal(body('HTTP')['jsonObj'].someProperty, null) --Unable to save
@equal(body('HTTP')['jsonObj']?.someProperty,'null') --Comparing with string value 'null'
Chestnut answered 16/8, 2016 at 13:50 Comment(1)
B
10

I did not found a real way to directly test against null or undefined but the following workaround should work when choosing a sufficient 'random' string as fallback for the coalesce

...
"propExists": "@equals(coalesce(triggerBody()?.prop, 'Fallback42'), 'Fallback42')"
...

For example the following Logic App would echo back the property prop and whether it was actually specified or not

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "actions": {
        "Response": {
            "inputs": {
                "body": {
                    "propNull": "@equals(coalesce(triggerBody()?.prop, 'undefined'), 'undefined')",
                    "prop": "@triggerBody()?.prop"
                },
                "statusCode": 200
            },
            "runAfter": {},
            "type": "Response"
        }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {},
    "triggers": {
        "request": {
            "inputs": {
                "schema": {}
            },
            "kind": "Http",
            "type": "Request"
        }
    }
}

so that a request with

{
    "prop": "test"
}

results in

{
  "prop": "test",
  "propNull": false
}

whereas a request with

{
    "propOther": "test"
}

results in

{
  "prop": null,
  "propNull": true
}
Bushman answered 16/8, 2016 at 16:49 Comment(0)
K
24

You can now do:

 @equals(triggerBody()['jsonObj']?['someProperty'], null)

It's valid and can be saved but if you try to switch to Basic mode you'll get an error. Can still save though.

Knipe answered 7/7, 2017 at 20:37 Comment(2)
You should be able to switch to basic mode without getting an error now as well. The null value is converted to an expression token whose value is null when shown in basic mode.Severalty
Use "is equal to" condition and fill in each box using the expression editor pop-up, don't type directly into the condition's fields themselves. On the left use: equals(triggerBody()['jsonObj']?['someProperty'], null) On the right use: truePermission
B
10

I did not found a real way to directly test against null or undefined but the following workaround should work when choosing a sufficient 'random' string as fallback for the coalesce

...
"propExists": "@equals(coalesce(triggerBody()?.prop, 'Fallback42'), 'Fallback42')"
...

For example the following Logic App would echo back the property prop and whether it was actually specified or not

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "actions": {
        "Response": {
            "inputs": {
                "body": {
                    "propNull": "@equals(coalesce(triggerBody()?.prop, 'undefined'), 'undefined')",
                    "prop": "@triggerBody()?.prop"
                },
                "statusCode": 200
            },
            "runAfter": {},
            "type": "Response"
        }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {},
    "triggers": {
        "request": {
            "inputs": {
                "schema": {}
            },
            "kind": "Http",
            "type": "Request"
        }
    }
}

so that a request with

{
    "prop": "test"
}

results in

{
  "prop": "test",
  "propNull": false
}

whereas a request with

{
    "propOther": "test"
}

results in

{
  "prop": null,
  "propNull": true
}
Bushman answered 16/8, 2016 at 16:49 Comment(0)
B
3

Another option is to do a string concatenation in the designer and check for a value > '' (a space.)

For example below I am iterating over a set of agents where their email is potentially NULL, joining a null string to an empty string results in an empty string.

This has the advantage of working in both the designer and code view.

@concat('', items('iterateAgents')?['email'])

This end up looking like the following in the code view

                "expression": {
                    "and": [
                        {
                            "greater": [
                                "@concat('', items('iterateAgents')?['email'])",
                                "  "
                            ]
                        }
                    ]
                },

2021 You can now also do the following in an if statement, check if the value is null with an EQUALS statement, and return out a known value if it is null, which you can check for. Otherwise return out the actual value you are interested in.

if( equals(body('basicEventJSON')?['type'], null), '_IS_NULL_VALUE',body('basicEventJSON')?['type'])
Belgae answered 28/5, 2020 at 13:54 Comment(0)
E
1

In my case I got three scenarios:

  • When property is something { "MyProperty" : "SomeValue" }
  • When property is null { "MyProperty" : null }
  • When property is not passed at all. { }

Suggested solutions didn't worked out for me, so I got those two simple expressions to cover three scenarios:

contains (triggerBody(), 'MyProperty') - Checks if the property exist with some value or null passed coalesce (triggerBody()?['MyProperty'], 'NULL') Uses property value or falls back to null case

Expressive answered 16/8, 2021 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.