问题描述
通过Azure Logic App(逻辑应用)实现无代码的处理JSON数据。但是如何获取Request Body中的一个属性值呢? 例如:如何来获取以下JSON结构中的 ObjectName 值?
Request对象中所含的Body内容(黄色高亮部分):
{ "headers": { "Connection": "close", "Accept": "*/*", "Accept-Encoding": "br,gzip,deflate", "Accept-Language": "en", "Host": "prod-24.chinaeast2.logic.azure.cn", "User-Agent": "Mozilla/5.0,(Windows NT 10.0; Win64; x64),AppleWebKit/537.36,(KHTML, like Gecko),Chrome/97.0.4692.99,Safari/537.36,Edg/97.0.1072.69", "x-ms-client-session-id": "22ea27e05a1b4637b2aa8f2b3a7a3039", "x-ms-command-name": "RunWithPayloadConsumptionBlade.runTrigger", "x-ms-effective-locale": "en.en-us", "x-ms-client-request-id": "b73db2d0-ec46-436d-bfef-a2d2b7e9a00d", "origin": "https://portal.azure.cn", "sec-fetch-site": "same-site", "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "Content-Length": "913", "Content-Type": "application/json" }, "body": [ { "id": "4be730e8-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "topic": "/subscriptions/xxxx-xxxx-xxxx-xxxx-xxxx/...", "subject": "sqlPassword", "eventType": "Microsoft.KeyVault.SecretNearExpiry", "data": { "Id": "......", "VaultName": "xxxx-kv", "ObjectType": "Secret", "ObjectName": "sqlPassword", "Version": "ee550dxxxx", "NBF": null, "EXP": 1643111997 }, "dataVersion": "1", "metadataVersion": "1", "eventTime": "2022-01-25T11:54:50.6358008Z" } ] }
如果直接在Logic App使用triggerBody()?['ObjectName'] 则遇见如下错误:
InvalidTemplate. Unable to process template language expressions in action 'Send_message' inputs at line '0' and column '0': 'The template language expression 'base64(triggerBody()?['objectName'])' cannot be evaluated because property 'objectName' cannot be selected. Array elements can only be selected using an integer index. Please see https://aka.ms/logicexpressions for usage details.'.
问题分析
从错误消息(Array elements can only be selected using an integer index)分析,因为 triggerBody() 函数获取的对象为一个Array(数组),所以无法查找到ObjectName属性。那么分析Request所传递的Body JSON结构后,发现:
1) Body中的最外层为一个数组,以中括号 [] 表示
2) Body中的第二层为一个对象,以大括号 {}表示
3) ObjectName属性位于data属性中,为第三层。
所以也无法使用 triggerBody()[0]?['objectName'] 加上数组索引后,依旧无法查找到ObjectName属性,错误信息如下:
InvalidTemplate. Unable to process template language expressions in action 'Send_message' inputs at line '0' and column '0': 'The template language function 'base64' expects its parameter to be a string, an object or an array. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#base64 for usage details.'.
问题解决
所以为了解决JSON中数组结构的取数问题,可以使用 Logic App的 Select Action 组件来完成数据的提取,但是如果能确定 HTTP Request Body中的数组只有一个元素,则可以在表达式中指定索引号或使用fist(), last()等函数获取数组中的对象。然后通过['属性名'] 来一级一级的获取数据,如本文中获取ObjectName属性值得正确表达式为:
triggerBody()[0]?['data']['objectName']
//或者
first(triggerBody())?['data']['ObjectName']
注:表达式中的“?”号表示如果triggerBody()[0]对象如果返回null或者undefined时,就直接返回null或 undefined,JSON操作不在执行后续对象data,objectName的查找
测试动画
1) 在Logic App中测试
2) 在Service Bus Topic中验证消息是否正确传递
参考资料
有关在 Azure 逻辑应用的表达式中使用函数的参考指南 : https://docs.microsoft.com/zh-cn/azure/logic-apps/workflow-definition-language-functions-reference#triggerBody