问题描述

通过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

【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?的更多相关文章

  1. 【Azure Developer】使用.Net Core解析JSON的笔记

    在C#中解析JSON的一些历史代码记录,分别记录针对各种情况的解析方式. DLL的引用 using Newtonsoft.Json; using Newtonsoft.Json.Linq; 需要使用的 ...

  2. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  3. Azure Logic App 入门(一)

    一,引言 前两天看一个azure相关的题,接触到一个叫 “Azure Logic App” 的服务,刚好,今天抽空学习以下,顺便结合它做一篇入门的分析文章. 首先,我们得对它有个大概的认识,了解以下A ...

  4. Azure Data Factory(四)集成 Logic App 的邮件通知提醒

    一,引言 上一篇有介绍到使用Azure Data Factory 复制数据,然后有集成 Azure DevOps 实现CI/CD,但是对于真正的项目来说,这些肯定是不够的,比如说在执行 Azure P ...

  5. 使用Azure API Management, Functions, Power Apps和Logic App构建应用

    ASP.NET OpenAPI 可以非常方便的将我们的Web API项目自动文档化,除了自动文档化以外,我们还可以利用Azure API Management将Open API自动文档化了的Web A ...

  6. Dynamics 365 Online-使用Azure Logic App 与 Dynamics 365 集成

    什么是Logic App? Azure Logic App 是微软发布的集成平台的产品,有助于生成,计划和自动完成工作流形式的流程,适合跨企业或组织集成,数据,系统和服务.与此同时,Logic App ...

  7. 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例

    当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...

  8. 【Azure Developer】App Service + PubSub +JS 实现多人版黑客帝国文字流效果图

    需要描述 1)实现黑客帝国文字流效果图,JS功能 2)部署在云中,让大家都可以访问,App Service实现 3)大家都能发送消息,并显示在文字流中,PubSub(websocket)实现 终极效果 ...

  9. 【Azure Developer】使用 Microsoft Authentication Libraries (MSAL) 如何来获取Token呢 (通过用户名和密码方式获取Access Token)

    问题描述 在上一篇博文<[Azure Developer]使用 adal4j(Azure Active Directory authentication library for Java)如何来 ...

随机推荐

  1. 细聊.NET6 ConfigurationManager的实现

    前言 友情提示:建议阅读本文之前先了解下.Net Core配置体系相关,也可以参考本人之前的文章<.Net Core Configuration源码探究>然后对.Net Core的Conf ...

  2. 【九度OJ】题目1179:阶乘 解题报告

    [九度OJ]题目1179:阶乘 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1179 题目描述: 输入n, 求y1=1!+3!+-m ...

  3. 【剑指Offer】把数组排成最小的数 解题报告(Python)

    [剑指Offer]把数组排成最小的数 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...

  4. Codeforces1132A——Regular Bracket Sequence(水题)

    Regular Bracket Sequence time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  5. 【服务器】【环境搭建】WordPress建立数据库连接时出错---问题---解决

    这意味着您在wp-config.php文件中指定的用户名和密码信息不正确,或我们未能在localhost联系到数据库服务器.这可能意味着您主机的数据库服务器未在运行. 您确定用户名和密码正确吗? 您确 ...

  6. Inverse/Implicit Function Theorem

    目录 4.1 The Inverse Function Theorem The Implicit Function Theorem 4.3 Curves and Surfaces 4.4 The Mo ...

  7. Null和空值对于avg计算时产生的影响以及处理

    为什么要关注这一块呢:1.面试中可能会有涉及 2.工作中真的也可能会用,既然有可能我也用过,就拿出来跟大家分享一下,上一篇的博文,数据已准备好就不做数据准备的介绍了. step1:select * f ...

  8. [GDOI2021 Day2T1] 宝石

    题目大意 \(n\)个点的树, 树上每一个点有一个宝石\(w_i\), 给出一个固定的数字不重复的序列\(p_i\)和一些询问\(u_i, v_i\), 对于每一个询问求出\(u_i\)到\(v_i\ ...

  9. Python爬虫脚本 ,Uni-APP复选框做出双向绑定 ,Net5工作流建模 。的一点经验

    从业C#开发多年,现在也经常用到Python 做网络爬虫 ,用Uni-app做手机前端.攒了一点经验.供其他多语言开发程序员借鉴吧. Python做爬虫和其他的方式做爬虫最大的区别应该在于. Pyth ...

  10. 灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor)

      灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor) 近日,国内领先的云原生全栈私有云提供商灵雀云宣布,推出企业版云原生制品仓库Ala ...