如何执行 'null' 签入 'if' Azure 逻辑应用程序的条件操作

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

我创建了一个逻辑应用程序,其中包含一些触发器、一个 'http' 连接器和一个 'If' 条件 activity。 'http' 连接器 returns 一个 'json' 结果说 jsonObj.

我能够检查 @equal(body('HTTP')['jsonObj'].someProperty,'someValue') 的条件,但无法对 someProperty 值进行 null 检查。

以下是我尝试过但无效的一些方法。

@equal(body('HTTP')['jsonObj'].someProperty, null) --Unable to save
@equal(body('HTTP')['jsonObj']?.someProperty,'null') --Comparing with string value 'null'

我没有找到直接针对 nullundefined 进行测试的真正方法,但在选择足够的 'random' 字符串作为 [=18 的回退时,以下解决方法应该有效=]

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

例如,以下逻辑应用程序将回显 属性 prop 以及是否实际指定了

{
    "$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"
        }
    }
}

所以

的请求
{
    "prop": "test"
}

结果

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

的请求
{
    "propOther": "test"
}

结果

{
  "prop": null,
  "propNull": true
}

您现在可以:

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

它是有效的并且可以保存,但是如果你尝试切换到基本模式,你会得到一个错误。仍然可以保存。

另一种选择是在设计器中进行字符串连接并检查值 > '' (a space.)

例如,在下面的示例中,我正在迭代一组代理,其中他们的电子邮件可能为 NULL,将空字符串连接到空字符串会导致空字符串。

这有利于在设计器和代码视图中工作。

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

这最终在代码视图中看起来如下所示

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

2021 您现在还可以在 if 语句中执行以下操作,使用 EQUALS 语句检查该值是否为 null,如果它为 null,则 return 输出一个已知值,您可以检查该值。否则return出你感兴趣的实际值。

if( equals(body('basicEventJSON')?['type'], null), '_IS_NULL_VALUE',body('basicEventJSON')?['type'])

就我而言,我遇到了三种情况:

  • 当 属性 是 { "MyProperty" : "SomeValue" }
  • 当属性为空时{ "MyProperty" : null }
  • 当属性根本没有通过。 { }

建议的解决方案对我没有用,所以我得到了这两个简单的表达式来涵盖三种情况:

contains (triggerBody(), 'MyProperty') - 检查 属性 是否存在并传递了某个值或空值 coalesce (triggerBody()?['MyProperty'], 'NULL') 使用 属性 值或退回到空大小写