问题描述

通过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. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  2. C# 编写 Windows 动态桌面软件实现(一)之桌面交互功能

    DreamScene2 1.3 版本已经发布了,现在支持鼠标和桌面交互功能.这个功能不会影响性能,基本不占用 CPU.这个功能让我对 Windows 消息机制有了更深入的理解,在这篇博客中我会详细介绍 ...

  3. Log4j 2.17.0 再曝漏洞,但不要惊慌!

    最新消息!根据Log4j官网发布,2.17.0版本还存在漏洞! 上图来自Log4j2官网:https://logging.apache.org/log4j/2.x/ 漏洞编号:CVE-2021-448 ...

  4. SOFA 通信

    私有通信协议设计: 我们的分布式架构,所需要的内部通信模块,采用了私有协议来设计和研发. 可以有效地利用协议里的各个字段 灵活满足各种通信功能需求:比如 CRC 校验,Server Fail-Fast ...

  5. rabbitmq-安装部署及基础操作

    rabbitmq 官网: https://www.rabbitmq.com/ yum 安装 rabbitmq # centos7 # In /etc/yum.repos.d/rabbitmq.repo ...

  6. [opencv]求像素范围中最大值与最小值

    double minv = 0.0, maxv = 0.0; double* minp = &minv; double* maxp = &maxv; minMaxIdx(channel ...

  7. 基于Spring MVC + Spring + MyBatis的【医院就诊挂号系统】

    资源下载:https://download.csdn.net/download/weixin_44893902/21727306 一.语言和环境 1.实现语言: JAVA语言. 2.环境要求: MyE ...

  8. CSS基础 水平居中案例

    html结构 <body> <div class="father"> <div class="son"></div&g ...

  9. 单例模式(python)

    python 的单例模式需要重写__new__()和 __init__() 需要注意,标识符__和_区别 参考资料: https://www.cnblogs.com/huchong/p/8244279 ...

  10. JMeter_性能压测报错address already in use:connect

    报错截图如下: 原因分析: 这个问题的原因是windows端口被耗尽了(默认1024-5000),而且操作系统要 2~4分钟才会重新释放这些端口,所以可以增加windows的可用端口来解决.windo ...