过滤查询中的Logic App绕过Null [英] Logic App bypass Null in filter query
问题描述
我正在构建一个使用Azure资源连接器的逻辑应用程序,以获取我的资源列表.然后,我想将结果过滤到标签名称为stop
和normal
的Microsoft.Compute
资源中.
I am building a Logic App that uses the Azure Resource connector to obtain a list of my resources. I would then like to filter the results to Microsoft.Compute
resources that have a tag name and value of stop
and normal
.
以下是我收到的资源摘要,没有任何过滤器
Here is a snippet of resource that I receive back without any filters
{
"id": "/subscriptions/<subscription>/resourceGroups/Env1/providers/Microsoft.Compute/virtualMachines/MyVM1",
"name": "MyVM1",
"type": "Microsoft.Compute/virtualMachines",
"location": "westeurope",
"tags": {
"stop": "normal"
}
},
{
"id": "/subscriptions/<subscription>/resourceGroups/LogicApp/providers/Microsoft.Logic/workflows/DWSize-Check",
"name": "DWSize-Check",
"type": "Microsoft.Logic/workflows",
"location": "westeurope",
"tags": {}
}
如您所见,底部资源不包含任何标签,就像列表中将出现的许多其他标签一样.
As you can see, the bottom resource does not contain any tags, as with many others that will appear in the list
我使用标准的Compose
Filter Array
连接器尝试从收到的value
中过滤掉.
I use the standard Compose
Filter Array
connector to try and filter out from the value
I receive back.
这是我希望用于filter命令的代码:
Here is the code that I wish to use for the filter command:
@and(contains(item()?['id'], '/Microsoft.Compute/virtualMachines/'), contains(item()?['tags'], variables('TagName')), contains(item()?['tags'], variables('TagValue')))
variables('TagName')
和variables('TagValue')
分别是stop
和normal
,如我的结果摘要中列出的示例标记所示.
variables('TagName')
and variables('TagValue')
will be stop
and normal
, as show in the example tags listed in my results snippet.
但是,由于在其他资源(例如Microsoft.Logic/workflows
)中没有列出标签值,因此出现以下null
错误:
However, because there is no tag values listed in other resources, such as Microsoft.Logic/workflows
, I receive the following null
error:
InvalidTemplate.模板动作"Filter_array"的执行 失败:对查询"操作何处"表达式的求值 '@contains(item()?['tags'],variables('TagValue'))''失败:' 模板语言函数包含"期望其第一个参数 集合"可以是字典(对象),数组或字符串.这 提供的值的类型为'Null'.
InvalidTemplate. The execution of template action 'Filter_array' failed: The evaluation of 'query' action 'where' expression '@contains(item()?['tags'], variables('TagValue'))' failed: 'The template language function 'contains' expects its first argument 'collection' to be a dictionary (object), an array or a string. The provided value is of type 'Null'.'.
有人知道如何解决这个问题吗?
Would anyone know how to get around this?
我曾尝试与此@contains(item().tags?.stop, variables('TagValue'))
类似的查询,只是看它是否拾取了任何东西,但我仍然被null
响应阻止:(
I have tried similar queries to this @contains(item().tags?.stop, variables('TagValue'))
just to see if it picks up anything, but I'm still blocked by a null
response :(
I tried the above with the help of the Workflow Definition Language, but still no dice. Any help would be greatly appreciated.
编辑
除了托马斯(Thomas)发布的答案外,我还执行了以下操作(下图),从结果中过滤出null
并转到TagName
,但是我仍然无法访问TagValue
,即使我使用包含:
In addition to the answer posted by Thomas, I have performed the following (image below) to filter out null
from the results and get to the TagName
, but I am still unable to get to the TagValue
, even if I use contains:
@and(contains(item()?['tags'], variables('TagName')), contains(item()?['tags'], variables('TagValue')))
或仅尝试查找TagValue
@contains(item()?['tags'], variables('TagValue'))
推荐答案
您可以检查null并返回一个空值(在您的情况下为一个空数组).
You can check for null and return an empty value (an empty array in your case).
您可以用此表达式替换item()?['tags']
或创建一个变量:
You can replace item()?['tags']
with this expression or create a variable :
if(equals(item()?['tags'], null), [], item()?['tags'])
这篇关于过滤查询中的Logic App绕过Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!