问题描述

通过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】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...

  2. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  3. 【LeetCode】519. Random Flip Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...

  4. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  5. 设计模式学习——JAVA动态代理原理分析

    一.JDK动态代理执行过程 上一篇我们讲了JDK动态代理的简单使用,今天我们就来研究一下它的原理. 首先我们回忆下上一篇的代码: public class Main { public static v ...

  6. wiodows /linux CMD

    windows : netstat -ano      查看所有端口使用情况 netstat -ano |findstr "端口号"     查看特定端口号 tasklist |f ...

  7. Redis常见使用场景

    缓存 数据共享分布式 分布式锁 全局ID 计数器 限流 位统计 购物车 用户消息时间线timeline 消息队列 抽奖 点赞.签到.打卡 商品标签 商品筛选 用户关注.推荐模型 排行榜 1.缓存 St ...

  8. Mac下搭建基于PlatformIO的嵌入式开发环境(STM32开发)

    PlatformIO简介 PlatformIO是开源的物联网开发生态系统.提供跨平台的代码构建器.集成开发环境(IDE),兼容 Arduino,ESP8266和mbed等 支持在Windows.Lin ...

  9. 在pycharm中创建py文件——创建你的第一个项目

    开启编程第一步   创建一个项目 创建项目了xdm,敲黑板了哈 首先打开你的pycharm,点击New Project新建项目 就会进入到配置你这个项目所要用到的环境,这里我们用python列举 在L ...

  10. MySQL数据库安装Version5.7.25

    1.说明 MySQL数据库大版本5.7的安装没有较大变化, 所以MySQL数据库Version5.7.25安装请参考:MySQL数据库安装Version5.7 上面文章中MySQL安装使用Versio ...